-4

I have c# property on server side and setting it on a HTTP request. Will that property value persist for another request? Sorry if it is silly one?

Kamil Budziewski
  • 22,699
  • 14
  • 85
  • 105
Bishnu Rawal
  • 1,387
  • 16
  • 33

2 Answers2

1

In general, no. It will persist if it's a property of something stored in your session variables (for example in your controller's TempData or in the application session data), or if you have some other custom mechanism that specifically preserves the value across requests.

Refer to the state management recommendations page on MSDN for an overview of your options.

Jon
  • 428,835
  • 81
  • 738
  • 806
1

Reference : A good to read article and for MVC

No, you can not access it if you are not saving it using these:

Asp MVC

TempData: Allows you to store data that will survive for a redirect. Internally it uses the Session as baking store, it's just that after the redirect is made the data is automatically evicted.

Syntax: TempData["foo"] = "bar";

ViewBag, ViewData: Allows you to store data in a controller action that will be used in the corresponding view. This assumes that the action returns a view and doesn't redirect. Lives only during the current request.

Syntax: ViewBag.Foo = "bar"; and ViewData["Foo"] = "bar";

ASP.Net

The Application Object: The Application object stores data that is shared across the application and not for a specific user. Whereas every page request gets its own Request and Response objects, all requests for ASP pages in a Web application share the same Application object. This object is created the first time an ASP page is requested from the application after the Web server starts up, and is destroyed when the Web server shuts down, or when the Web application is unloaded manually in the IIS management console. Because this object persists from one page request to another, it can be used to store data that you want to share with all other pages in your application.

Syntax: Application(“varName”) = value

The Session Object: The Session object is very similar to the Application object, as it allows you to store values that are shared between all the pages of your site. The main difference between the two is that, where a single Application object is shared by all pages and all clients that access your site, each client (browser) is assigned its own Session object. Thus, a Session object must be created for each user session that occurs on your Website.

Syntax: Session(“username”) = “Abhinav bajpai”

The View State: The ViewState allows ASP.NET to repopulate form fields on each postback to the server, making sure that a form is not automatically cleared when the user hits the submit button. All this happens automatically, unless you turn it off, but you can actually use the ViewState for your own purposes as well. Please keep in mind though, that while cookies and sessions can be accessed from all your pages on your website, ViewState values are not carried between pages. StateBag implements the view state and manages the information that ASP.NET pages and embedded controls persist across successive posts of the same page instance.

Syntax: ViewState(“FontSize”) = value

Cookie: Cookie is one of several ways to store data about web site visitors during the time when web server and browser are not connected. Common use of cookies is to remember users between visits. Practically, cookie is a small text file sent by web server and saved by web browser on client machine.

Syntax: Response.Cookies["MyCookieName"].Value=“MyCookieValue”;

Querystring: Query string is used to pass the values or information form one page to another page.

Syntax: Request.QueryString(variable)[(index)|.Count]

Community
  • 1
  • 1
Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110