Sessions Flashcards
What is a session?
The user interaction when the user opens a browser, does some tasks and closes the browser is called a session.
For example, when the user opens a browser, logs in, then adds some products, checkout and closes the browser. This is called a session.
Explain “HTTP is a stateless protocol” ?
HTTP does not remember states between the request and the response. User sends the request, server processes it and responds. When a user sends another request in the same session, the server consider it as a new request.
For example, create a member variable, i, in the controller class. Increment this in any action method, this won’t be persisted in next request.
What are various way of doing session management ?
OR
How do you maintain states in HTTP request?
There are three ways:
Tempdata
Viewdata/ViewBag
Sessions variables
Are sessions enabled by default ?
No.
How to enable sessions in MVC core ?
In ConfigureServices, add
servcies. AddSession( options = >
options. idleTImeut = TimeSpan.Minutes(15));
in Configure methods,
app.UseSession()
To read a session, HTTPContext.Session.GetString(“name”);
to set,
HTTPContext.Session.SetString(“name”, “value”);
Are sessions variables shared(global) between users ?
No
Do session variables use cookies ?
Yes
What is a cookie ?
They are small text files that stored in the end userss’ machines.
Explain idle time out in sessions ? ….
servcies. AddSession( options = >
options. idleTImeut = TimeSpan.Minutes(15));
What does a Context means in HTTP ?
one request and response is called a context. HTTPContext holds information regarding sessions, request data and response information. It has all data required for one request and response.
When should we use viewdata ?
How to pass data from controller to view ? .
use viewData
in controller action method
{
Viewdata[“vd”] = 5
}
In the HTML file use
@ViewData[“vd”]
In same request can viewdata persist across actions ?
No…
ViewBag vs ViewData ?
ViewBag is a synthetic sugar to ViewData.
ViewData[“vd”] = ViewBag.vd
How does ViewBag work internally ?
it uses dynamic and if viewbag’s property is not existing then it returns null.
ViewBag vs ViewModel Whats the best practice ?
use strongly types such as ViewModel