2014-04-08 42 views
1

在我的應用程序已包括在Web.config中<customerror>標籤如下:自定義錯誤重定向無法在ASP.NET MVC的Web.Config工作

<customErrors mode="On"> 
    <error statusCode="404" redirect="/Error/404"/> 
    <error statusCode="403" redirect="/Error/403"/> 
    <error statusCode="500" redirect="/Error/500"/> 
</customErrors> 

我已經創造了我的路線配置的條目,以使得與URL模式/錯誤/ {狀態}任何請求進入到一個特定的控制器動作用狀態作爲參數。

而且我有檢查用戶是否是admin,返回HTTP 403的結果,如果不是自定義ActionFilterAttribute。以下是我的自定義過濾器屬性。現在

public class RequireAdminAttribute: ActionFilterAttribute 
{ 
    public override void OnActionExecuted(ActionExecutedContext filterContext) 
    { 
     if (!UserOperations.IsAdmin()) 
     { 
      filterContext.Result = new Http403Result(); 
     } 
    } 
} 

internal class Http403Result : ActionResult 
{ 
    public override void ExecuteResult(ControllerContext context) 
    { 
     // Set the response code to 403. 
     context.HttpContext.Response.StatusCode = 403; 
    } 
} 

,我需要的是用戶被重定向到/錯誤/ 403頁,如果他沒有被授權。當我運行我的應用程序時,當403錯誤發生時,我的應用程序不會重定向到錯誤頁面。如果發生404錯誤,它將重定向到給定的404錯誤頁面(在大多數情況下,404發生時)。這是什麼原因?任何人都可以給我一個解決方案嗎?我是否需要使用像RedirectToRouteResult這樣的硬編碼重定向到錯誤頁面?

編輯: 在某些情況下重定向不工作,我明確地使用return HttpNotFound();結果也是如此。我想知道是否使用return HttpNotFound();不會重定向到自定義頁面。

謝謝。

+0

退房http://stackoverflow.com/questions/16251134/where-does-customerrors-in-web-config-go-for-mvc-應用程序 – defcde

回答

3

嘗試將此添加到您的Web.config

<system.webServer> 
    <httpErrors existingResponse="Replace" errorMode="Custom" defaultResponseMode="Redirect"> 
    <remove statusCode="403"/> 
    <error responseMode="ExecuteURL" statusCode="403" path="/Error/403" /> 
    </httpErrors> 
</system.webServer> 
+0

我將'error'標記中的回答中提到的路徑'屬性'替換爲'/ Error/403'並重試。但沒有運氣。 – Deepal

+0

結果如何?另外,什麼版本的IIS? – haim770

+0

它的IIS 8,還顯示詳細的錯誤頁面。 – Deepal

相關問題