ASP.NET :Events in a life cycle of a Web Application
Events in the life cycle of a Web Application
For example, if you define ViewState["MyData"] = "View State Example" in WebForm1. ViewState["MyData"] is only available in WebForm1. ViewState["MyData"] will be null on any other web form in the application.
Now, let's write a simple application, using session and application level events. Create a new asp.net web application, and copy paste the following code in Global.asax file.
1. Application_Start() event gets fired, when a first request is made, and if the application is not already running.
2. Session_Start() event is fired every time a new browser instance, with a different session-id, visits the application.
3. Session_End() event is fired when the user session times out. The default is 20 minutes. This can be configured in the web.config file.
So we go to run it and press CTRL + F5
Copy the URL and open a new instance of the browser. Paste the URL and press enter. In the new instance of the browser, we still see the same output.
We expected the Number of Users Online to be 2. The new instance of the browser, is treated as part of the same session, because, by default the browser uses cookies to store session id. The session id is read from the same cookie when you opened the new browser window. Hence, Number of Users Online is not incremented.
This happens when we close the session and we open it up again:the session ID sees it as a New user...and Start the Session
1. Close the browser: Close the existing browser window, which automatically deletes the session cookie. Now, open a new brwoser instance. Since, the existing session cookie associated with the previous browser instance is deleted. The new instance of the browser, will get a new session-id and a session cookie.Now, if you navigate to WebForm1.aspx, Session_Start() event gets fired and Number of Users Online is incremented to 2.
2. Open a new instance of a different browser: For example, if you first visited the application with Google Chrome, now try accessing the same page with internet explorer, Session_Start() event gets fired and Number of Users Online is incremented to 2.
3. Use Cookie-less Sessions: To use cookie-less sessions set the cookieless attribute to true in web.config as shown below.
<sessionState mode="InProc" cookieless="false"></sessionState>
You can open a new browser vendor..Different session ID..The web browser thinks it s a different user..
ASP.NET :Events in a life cycle of a Web Application
Reviewed by ohhhvictor
on
12:15 PM
Rating:

No comments: