I think I found a problem with ASP.NET MVC and the event pipeline. In particular, I find that Session_Start is called several times, each of which contains a new SessionID.
Here step by step:
- Open VS2010
- File | New project
- ASP.NET MVC 3 web application, provide a default name, click OK
- Select an Internet application (although I donβt think itβs really important), click OK
- When creation is complete, edit the Global.asax.cs file
Add the following method (yes it is empty):
protected void Session_Start () {}
Set a breakpoint in the method
- Debug
- Note that the breakpoint is caught twice before the page is displayed. If you see "Session.SessionID" when breakpoints are caught, you will see that the session identifier is new every time.
- As soon as you go to the home page, click on the link "Home" or "About".
- Session_Start will be started again, this time with a new SessionID.
- Continue execution, and any subsequent actions will no longer trigger Session_Start.
I tried the same thing on a standard ASP.NET web application (and not MVC), and Session_Start was run only once.
I am sure that I am not doing something wrong here, as I use the default project templates, and the only modifiable code is the Global.asax.cs file to add the Session_Start method.
I am using IIS Express, but I repeated the above steps using the Cassini web server (Visual Studio Development Server) with the same result.
Any tips?
UPDATE
I decided to use Fiddler to check HTTP traffic during a debugging session. It seems that:
- The first Session_Start is fired when I request a url. That seems reasonable. Then, the SessionID generated at this time is recorded in response to the browser. Again, it seems reasonable.
- Fiddler then shows the requests / responses for the * .js and * .css files. All success. None of them disable Session_Start. Still.
- Fiddler then shows that "/favicon.ico" was requested. At this time, Session_Start starts and generates a new SessionID ... I continue.
- In Fiddler, it shows that the file "/favicon.ico" was not found (404). A web page is displayed. I click on the "Home" link.
- The URL "/" is requested, and in Fiddler the response is OK. But then another "/favicon.ico" file is requested, and again Session_Start starts with the new SessionID ... I continue.
- All subsequent requests have answers, and the browser stops requesting "/favicon.ico".
I drew attention to each of the three generated SessionID, and it seems that the first one contains a browser. Therefore, when we go to step 6 above, and everything seems to work, it actually uses the very first SessionID that was generated.
So ... I decided to place the file "favicon.ico". I put the ico file in the root directory of the project and started a debugging session again. This time, Session_Start runs only once. "/favicon.ico" filed successfully (200).
So ... I think it works as it should in a sense ... But why do calls to /favicon.ico drive the Session_Start event ???? Should I have a choice not to post icons?
ASIDE: I tried everything above in an ASP.NET project (not mvc), and it did not have the same problem, although the favicon.ico file, hosted by default "ASP.NET Web Application", was missing, the project.