404重定向仅用于页面而不是图像

ama*_*eur 3 asp.net asp.net-mvc http-status-code-404 asp.net-mvc-4

我正在使用一个 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>
Run Code Online (Sandbox Code Playgroud)

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

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

imv*_*in2 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>
Run Code Online (Sandbox Code Playgroud)