Sessions Flashcards

1
Q

What is a session?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Explain “HTTP is a stateless protocol” ?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are various way of doing session management ?

OR

How do you maintain states in HTTP request?

A

There are three ways:

Tempdata

Viewdata/ViewBag

Sessions variables

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Are sessions enabled by default ?

A

No.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to enable sessions in MVC core ?

A

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”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Are sessions variables shared(global) between users ?

A

No

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Do session variables use cookies ?

A

Yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a cookie ?

A

They are small text files that stored in the end userss’ machines.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Explain idle time out in sessions ? ….

A

servcies. AddSession( options = >

options. idleTImeut = TimeSpan.Minutes(15));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does a Context means in HTTP ?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

When should we use viewdata ?

How to pass data from controller to view ? .

A

use viewData

in controller action method

{
Viewdata[“vd”] = 5
}

In the HTML file use

@ViewData[“vd”]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

In same request can viewdata persist across actions ?

A

No…

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

ViewBag vs ViewData ?

A

ViewBag is a synthetic sugar to ViewData.

ViewData[“vd”] = ViewBag.vd

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How does ViewBag work internally ?

A

it uses dynamic and if viewbag’s property is not existing then it returns null.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

ViewBag vs ViewModel Whats the best practice ?

A

use strongly types such as ViewModel

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is ViewModel?

A

to create a ViewModel, create a class called

ViewModelClass
{

}

Populate its data in the Action method.

Then in the view, @model modelname

@Model.properties

17
Q

Explain tempdata ?

A

tbd

18
Q

How is tempdata different from viewdata ?

A

ViewData is not persist from action to action and accross requests. in tempdata we can control its persistence.

19
Q

If tempdata is read is it available for next request ?

A

Value ispersist accross request until it it read

20
Q

How to persist tempdata ?
or
What does Keep do in tempdata ?…

A

use Keep method. Keep don’t return anything.

21
Q

Explain Peek in tempdata ?

A

read + Keep is Peek

22
Q

How is tempdata different from session variables ?

A

Session vairble stays until the entire session

23
Q

How is tempdata different from session variables ?

A

Session variables stays until the entire session

24
Q

Is tempdata private to a user ?.

A

yes

25
Q

ViewData vs ViewBag vs Tempdata vs Session variables

A

TBD