在session_start上默认的ASP.NET MVC3项目射击多次 [英] Session_Start firing multiple times on default ASP.NET MVC3 project
问题描述
我想我可能已经找到了ASP.NET MVC的一个问题,它的事件管线。特别是,我发现在session_start被多次调用,每个都包含一个新的SessionID。
I think I may have found a problem with ASP.NET MVC and it's event pipeline. In particular, I am finding that Session_Start is being called multiple times, each containing a new SessionID.
这里是一步一步的过程:
Here's the step-by-step process:
- 开启VS2010
- 文件|新建项目
- ASP.NET MVC 3 Web应用程序,接受默认名称,然后单击确定
- 选择互联网应用程序(虽然我不认为这真的很重要),然后单击确定
- 当完成创建,编辑的Global.asax.cs文件
-
添加以下方法(是的,它是空的):
- Open VS2010
- File | New Project
- ASP.NET MVC 3 Web Application, accept default name, click OK
- Select Internet Application (although I don't think it matters really), click OK
- When finished creating, edit the Global.asax.cs file
Add the following method (yes it's empty):
保护无效在session_start()
{
}
protected void Session_Start() { }
在该方法中设置断点
我想同样的事情在一个标准的ASP.NET Web应用程序(不MVC),并在session_start只能触发一次。
I tried the same thing on a standard ASP.NET Web Application (not MVC), and Session_Start only fired once.
我是pretty肯定我不会在这里做得不对,因为我使用的是默认的项目模板,并且正在修改的唯一code是的Global.asax.cs文件,以添加在session_start方法。
I'm pretty sure I'm not doing something wrong here, as I am using the default project templates, and the only code that is being modified is the Global.asax.cs file, to add the Session_Start method.
我使用IIS防爆preSS,但我已经重复使用卡西尼Web服务器(Visual Studio的开发服务器)上面的步骤,具有相同的结果。
I am using IIS Express, but I've repeated the above steps using the "Cassini" web server (Visual Studio Development Server), with the same result.
任何意见?
我决定用我的提琴手调试会话期间,检查HTTP流量。似乎
I decided to use Fiddler to inspect the HTTP traffic during my debug session. It seems that:
- 第一个在session_start是当我请求/URL解雇。这似乎是合理的。当时生成的会话ID,然后写在响应于浏览器。再次,似乎是合理的。
- 菲德勒则显示了的* .js和* .css文件请求/响应。所有的成功。这些都不关火在session_start的。好为止。
- 然后提琴手表明,请求已为/favicon.ico进行。这时,在session_start火灾,并生成一个新的SessionID ......我继续。
- 在提琴手,它表明/favicon.ico找不到文件(404)。显示该网页。我点击主页链接。
- 的URL/请求和响应是提琴手确定。但随后,另一个/favicon.ico的文件要求,并再次在session_start用新的SessionID火灾......我继续。
- 所有后续请求有响应,浏览器将停止要求/favicon.ico。
我留意这三个的SessionID的的生成,它似乎是浏览器保存到一个是第一位的。所以,当我们拿到第6步以上,一切似乎工作,它的使用实际上是生成的第一的SessionID。
I made note of each of the three SessionID's generated, and it seems the one that the browser holds on to is the first one. So when we get to step 6 above, and everything seems to work, it's actually using the very first SessionID that was generated.
所以...我决定举办一个的favicon.ico文件。我把ICO文件中的项目的根目录,然后再次开始了我的调试会话。这一次,在session_start只能触发一次。 /favicon.ico被成功地服务(200)。
So... I decided to host a "favicon.ico" file. I placed the ico file in the root of the project, and started my debug session again. This time, Session_Start only fires once. "/favicon.ico" was served successfully (200).
所以...我想这是工作它应该在一定意义上的方式......但是,为什么呼吁/favicon.ico关火在session_start事件????我不应该有选择地不举办一个图标?
So... I guess it is working the way it should in a sense... But why do calls to "/favicon.ico" fire off the Session_Start event???? Shouldn't I have the choice to NOT host a favicon?
题外话:我想在ASP.NET(不MVC)项目中的所有之上,它没有同样的问题,即使有一个默认的ASP.NET Web应用程序托管没有任何文件的favicon.ico项目。
ASIDE: I tried all the above in an ASP.NET (not mvc) project, and it did not have the same problem, even though there was no favicon.ico file hosted by a default "ASP.NET Web Application" project.
推荐答案
我想我已经到了一个地步,我有一对夫妇的解决方案(尽管双方似乎哈克对我来说),所以我想我会接受这些,继续前进。
I think I've come to a point where I have a couple of solutions (albeit both seem 'hacky' to me), so I think I'll accept these and move on.
得到@Tz_评论上面提到的我应该忽略的图标文件的路径。这是本质是什么,我会做的事情。 (荣誉@Tz _!)
Got a comment from @Tz_ above that mentioned I should ignore the route for the favicon file. That's essentially what I'll be doing. (kudos @Tz_!)
遇到下列<少时href=\"http://stackoverflow.com/questions/4624190/mvc-does-the-favicon-ico-also-look-for-a-controller\">post, (等等)。它描述了一个问题,即当浏览器从一个ASP.NET MVC网站请求/favicon.ico文件中,MVC堆栈的 mistakingly 的试图寻找和实例化一个控制器。我不知道,如果这是真的还是假的了我的情况,但得到的答复建议增加以下路由条目:
Came across the following post, (among others). It describes a problem that when the browser requests a "/favicon.ico" file from an ASP.NET MVC site, the MVC stack is mistakingly trying to look for and instantiate a controller. I'm wasn't sure if that was true or not for my situation, but the answer suggested adding the following route entry:
routes.IgnoreRoute("favicon.ico");
我给它一个镜头(添加了以上),并固定它!
I gave it a shot (added the above), and that fixed it!
所以,我还是不知道的为什么的/favicon.ico要求在MVC中的张冠李戴,但我知道如何解决它在我的处境。或者:
So, I still don't know why "/favicon.ico" request has a mistaken identity in MVC, but I know how to fix it in my situation. Either:
- 主机上的图标,
- 或添加忽略路由表项。
此外,无论是看起来像黑客对我来说,因为我认为这是一件控制器工厂应该能够处理正常。恕我直言
Again, both seem like hacks to me, as I think this is something controller factories should be capable of handling gracefully. IMHO
这篇关于在session_start上默认的ASP.NET MVC3项目射击多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!