3

我正在使用一个 asp.net mvc 应用程序。我在 web.config 中有以下条目来处理 404

    <httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" path="/Error/Error404" responseMode="ExecuteURL" />
</httpErrors>

这适用于请求页面时,它重定向到我的 404 视图。但是,对于丢失的图像,它也会重定向到 404 页面,即。图像的响应是 404 页面。

由于这是一个性能问题,有什么办法可以改变上述内容,以便只有“页面”中的 404 而不是图像等资源触发重定向到 404 页面?

4

2 回答 2

5

我知道这是一个老问题,但是我刚刚遇到了同样的问题,并且我能够根据我在 htaccess 中使用的内容提出一个解决方案。

下面的解决方案基本上检查文件是否具有图像扩展名,它不是文件也不是目录。之后,它会提供一个图像文件,我用它来识别丢失的图像。

<rule name="404 Images" stopProcessing="true">
    <match url=".*" ignoreCase="true" />
    <conditions>
        <add input="{URL}" pattern="^.+\.(jpg|jpeg|png|gif|ico)$" ignoreCase="true" negate="false" />
        <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="true" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="true" negate="true" />
    </conditions>
    <action type="Rewrite" url="/design/images/404.png" />
</rule>
于 2014-05-28T15:20:36.347 回答
4

您可以禁用runAllManagedModulesForAllRequests

<system.webServer>
    <modules runAllManagedModulesForAllRequests="false" />
    ...
</system.webServer>

当然,现在您将看到 IIS 默认 404 页面显示损坏的图像,因为静态资源将直接由静态处理程序提供服务,而不是通过托管管道。

于 2013-03-24T13:57:12.083 回答