创建自定义 IHttpModules,我意识到对静态文件(例如:.css 和 .js 文件)的请求正在命中托管模块。可能图片也有同样的问题。对于文件系统中存在的文件,IIS 不应该绕过 ASP.NET 吗?
例如:
public class MyModule:IHttpModule
{
public void Dispose(){ }
public void Init(HttpApplication context)
{
context.BeginRequest += (o, e) => Debug.Print("Request: " + HttpContext.Current.Request.RawUrl);
}
}
我这样声明:
<modules runAllManagedModulesForAllRequests="true">
<add name="MyModule" preCondition="managedHandler" type="MVCX.Modules.MyModule, MVCX"/>
</modules>
但是,即使使用前提条件,我也可以看到静态文件如何通过模块:
Request: /MVCX/
Request: /MVCX/Content/Site.css
Request: /MVCX/Scripts/jquery-1.4.4.min.js
我试图忽略静态文件的规则,但这并没有什么不同:
routes.IgnoreRoute("{Content}/{*pathInfo}");
routes.IgnoreRoute("{Scripts}/{*pathInfo}");
这是平常的吗?或者我在这里错过了什么?据我所知,如果静态文件请求应该由 IIS 回答。如果我的托管模块被命中,则意味着 CLR ThreadPool 线程正在处理该请求,对吗?
问候。
更新:
我已禁用“runAllManagedModulesForAllRequests”:
<modules runAllManagedModulesForAllRequests="false">
<add name="MyModule" preCondition="managedHandler" type="MVCX.Modules.MyModule, MVCX" />
</modules>
一切似乎都很好,但我发现这篇文章:http ://www.britishdeveloper.co.uk/2010/06/dont-use-modules-runallmanagedmodulesfo.html 建议删除并阅读“UrlRoutingModule-4.0 " 具有空前提条件的模块。
在我的机器上,该模块的添加位于根 web.config 中,并且它已经有一个空的前置条件:
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config>type machine.config | find "UrlRouting"
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config>type web.config | find "UrlRouting"
<add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" />
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config>
所以现在我有点困惑,这个参数的状态是什么?我应该使用它还是不应该使用它?为什么默认情况下它是“真实的”?
问候。