对静态文件的请求正在访问 ASP.NET MVC3 中的托管代码 [英] Requests for static files are hitting the managed code in ASP.NET MVC3

查看:31
本文介绍了对静态文件的请求正在访问 ASP.NET MVC3 中的托管代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建自定义 IHttpModules,我意识到对静态文件(例如:.css 和 .js 文件)的请求正在命中托管模块.可能图片也有同样的问题.对于文件系统中存在的文件,IIS 不应该绕过 ASP.NET 吗?

Creating custom IHttpModules, I have realized that the requests for static files (e.g.: .css and .js files) are hitting the managed modules. Probably pictures have the same problem. Shouldn't IIS bypass ASP.NET for files that exists in the filesystem?

例如:

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>

但是,即使使用前提条件,我也可以看到静态文件如何通过模块:

But, even using the precondition I can see how the static files goes through the module:

Request: /MVCX/
Request: /MVCX/Content/Site.css
Request: /MVCX/Scripts/jquery-1.4.4.min.js

我试图忽略静态文件的规则,但没有任何区别:

I have tried to ignore the rules for static files, but it does not make a difference:

routes.IgnoreRoute("{Content}/{*pathInfo}");
routes.IgnoreRoute("{Scripts}/{*pathInfo}");

这是平常的事吗?或者我在这里错过了什么?据我所知,如果静态文件请求应该由 IIS 回答.如果我的托管模块被命中,则意味着 CLR ThreadPool 线程正在处理该请求,对吗?

Is this the usual? Or am I missing something here? As far as I know, if the static file request should be answered by IIS. If my managed module is being hit, means that a CLR ThreadPool thread is handling that request, right?

问候.

更新:

我已禁用runAllManagedModulesForAllRequests":

I have disabled the "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"模块.

And everything seems to work just fine, but I have found this article: http://www.britishdeveloper.co.uk/2010/06/dont-use-modules-runallmanagedmodulesfo.html that recommends remove and readd the "UrlRoutingModule-4.0" module with an empty precondition.

我的机器,该模块的添加在根web.config中,并且它已经有一个空的前置条件:

I my machine, the adding of that module is in the root web.config, and it has already an empty preCondition:

C:WindowsMicrosoft.NETFramework64v4.0.30319Config>type machine.config | find "UrlRouting"


C:WindowsMicrosoft.NETFramework64v4.0.30319Config>type web.config | find "UrlRouting"
            <add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" />

C:WindowsMicrosoft.NETFramework64v4.0.30319Config>

所以现在我有点困惑,这个参数的状态是什么?我应该使用它还是不应该使用它?为什么它默认为true"?

So now I am a little bit confused, what is the status of this parameter? Should I use it or shouldn't? why does it come as "true" by default?

问候.

推荐答案

我的机器,该模块的添加在根web.config中,并且它已经有一个空的preCondition

I my machine, the adding of that module is in the root web.config, and it has already an empty preCondition

完美.这意味着该模块将始终运行,这是 MVC 所必需的,因为它使用无扩展 url.

Perfect. That means this module will always run which is required for MVC as it uses extensionless urls.

所以现在我有点困惑,这个参数的状态是什么?我应该使用它还是不应该使用它?为什么它默认为true"?

So now I am a little bit confused, what is the status of this parameter? Should I use it or shouldn't? why does it come as "true" by default?

因为无扩展 url 支持是 IIS7 SP1 和 IIS7.5 SP1 中的新功能.它可用于 IIS7 作为您必须请求和安装的补丁程序.您可以在这里找到问题的完整答案:http://support.microsoft.com/kb/980368

Because extensionless url support is new in IIS7 SP1 and IIS7.5 SP1. It is available for IIS7 as a patch that you have to request and install. You will find it here with complete answers to your questions: http://support.microsoft.com/kb/980368

为什么这个参数默认为true?因为 VS2010 是在 IIS7 SP1 之前发布的.也许它在 VS2010SP1 的新 MVC 项目中是错误的?

Why this parameter comes to true by default ? Because VS2010 was shipped before IIS7 SP1. Maybe it is at false in new MVC projects in VS2010SP1 ?

这篇关于对静态文件的请求正在访问 ASP.NET MVC3 中的托管代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆