2013-03-24 98 views
1

我正在使用asp.net mvc應用程序。我在web.config中的以下條目處理404的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頁面?

+0

你的問題還不清楚。 「頁面」是什麼意思?你想要它處理哪些網址? – SLaks 2013-03-24 13:57:53

回答

4

您可以禁用runAllManagedModulesForAllRequests

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

當然現在你會看到IIS默認404頁破碎的形象,因爲靜態資源會被靜電處理直接送達,並通過託管管道不會。

+0

感謝你 - 任何方式,我可以關閉它的圖像只等等或另一種解決方法。對於我的網站中的其他功能,我需要打開此功能。 – amateur 2013-03-24 14:22:41

+1

只有在IIS中沒有像* images *這樣的概念。有靜態和管理資源。你可以編寫一個通用的處理程序或控制器來爲你的圖像提供服務,並擁有更多的控制權,但這是你真正需要的嗎? – 2013-03-24 14:23:27

3

我知道這是一個古老的問題,但我剛剛遇到同樣的問題,我能夠根據我在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> 
相關問題