Why http protocol is called stateles protocol? Explain different strategies, we use in .Net core to hold state of application. Give suitable example.
The HTTP protocol is called a stateless protocol because it only is inactive state until it gets the request and searches for response in the database. By the time, it gets data and responds with the result to the request it closes all the connections with both user and the database instantly.
I part
The different strategies/techniques we use in •Net core to hold the state of application are:(state management techniques)
1)Client-side :view state/Hidden Fields/Cookies/Query String
1)Server-side: Application state/Session state
Client-side Strategies
a)View State
- View state is most used when user wants to maintain and store their data temporarily in ASP.NET applications.
- It can store any type of data
b)Hidden Fields
- IT used for storing small amounts of id-data on the client-side.
- Invisible in the browser.
- Provide direct functionality access.
- We can save data in hidden form fields and send it back in the next request.
- Sometimes we require some data to be stored on the client-side without displaying it on the page. Later when the user takes some action, we’ll need that data to be passed on to the server-side.
- This is a common scenario in many applications and hidden fields provide a good solution for this.
- Let’s add two methods in our WelcomeController:
[HttpGet]
public IActionResult SetHiddenFieldValue() {
User newUser = new User() {
Id = 101, Name = "John", Age = 31
};
return View(newUser);
}
[HttpPost]
public IActionResult SetHiddenFieldValue(IFormCollection keyValues) {
var id = keyValues["Id"];
return View();
}
c) Cookies
- A small file that is stored in the user hard drive using the client's browser.
- It stores information temporarily.
- It can be changed according to requirements.
Examples
Reading Cookie
//read cookie from IHttpContext Accessor
string cookieValueFromContext = httpContextAccessor.HttpContext.Request.Cookies["key"];
//read cookie from Request object
string cookieValueFromReq = Request.Cookies[“key"];
Remove Cookie
Response.Cookies.Delete(key);
Writing cookie
- In this example, SetCookie method show how to write cookies.
- CookieOption is available to extend the cookie behavior.
public void SetCookie(string key, string value, int? expireTime) {
CookieOptions option = new CookieOptions();
if (expireTime.HasValue)
option.Expires = DateTime.Now.AddMinutes(expireTime.Value);
else
option.Expires = DateTime.Now.AddMilliseconds(10);
Response.Cookies.Append(key, value, option);
}
d) Query String
- It is generally used for holding values
- It works temporarily
- It increases the performance of the app.
Example
We can pass a limited amount of data from one request to another by adding it to the query string of the new request. This is useful for capturing the state in a persistent manner and allows the sharing of links with the embedded state.
public IActionResult GetQueryString(string name, int age) {
User newUser = new User()
{
Name = name,
Age = age
};
return View(newUser);
}
Now let’s invoke this method by passing query string parameters:
/welcome/getquerystring?name=John&age=31
Server Side
a)Application state
- Global storage to store the values that are accessible to all pages.
- It stores data in the form of key-value pairs.
b)Session state
- IT stores the values that are accessible throughout the application only in the current browser.
- It cant be accessible from other browsers and is used sometimes to store sensitive values or data.
- Let’s create a controller with endpoints to set and read a value from the session:
public class WelcomeController: Controller
{
public IActionResult Index() {
HttpContext.Session.SetString("Name", "John");
HttpContext.Session.SetInt32("Age", 32);
return View();
}
public IActionResult Get()
{
User u = new User()
{
Name= HttpContext.Session.GetString("Name"),
Age= HttpContext.Session.GetInt32("Age").Value
};
return View(u);
}
}
Comments
Post a Comment