Application_BeginRequest未在生产服务器上为JPG触发

ant*_*tfx 5 .net c# iis

我有一个IIS 7.5网站在经典管道模式下运行.NET 4.0.

我创建了一个简单的默认图像设置,如果调用了图像但物理文件不存在,则在Application_BeginRequest事件中使用以下代码将请求重定向到默认图像:

protected void Application_BeginRequest(object sender, EventArgs e)
{
    if (Request.PhysicalPath.EndsWith(".jpg") && File.Exists(Request.PhysicalPath) == false)
    {
        Context.RewritePath("~/images/nophoto.jpg");
    }
}
Run Code Online (Sandbox Code Playgroud)

这在我的VS2010开发服务器上工作正常,但是在生产环境中,没有为JPG请求调用Application_BeginRequest事件,我得到的是标准HTTP错误404.0 - 未找到错误.

我已经尝试将runAllManagedModulesForAllRequestsWeb.Config中的选项设置为true但这似乎没有帮助:

<system.webServer>
    <modules runAllManagedModulesForAllRequests="true"></modules>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)

根据我的理解,这应该导致所有请求通过.NET,从而触发Application_BeginRequest事件?

期望的结果:

我希望所有请求都通过.NET,以便为JPG调用Application_BeginRequest事件,如果没有找到图像则返回默认图像.

Tam*_*eer 3

经典模式不会发生这种情况,您需要切换到集成模式。

这篇文章可能会提供一些见解。