2016-08-17 81 views
4

我試圖讓我的404s正常工作,但無法弄清楚如何得到它非常正確。返回404頁面無重定向

最初我把我在我的Statup配置方法如下:

app.UseMvc(routes => 
{ 
    // routing here 
}); 

app.Use((context, next) => 
{ 
    context.Response.StatusCode = 404; 
    return next(); 
}); 

app.UseStatusCodePagesWithRedirects("/error/{0}"); 

哪個重定向到一個頁面,在這裏我發現了錯誤。但狀態代碼是302 > 200。我設置/error/{code}行動返回相關的狀態代碼,所以現在我有302 > 404其中(由於302重定向),使它看起來好像/error/404頁面不存在(它的確如此)。

我想要做的是返回沒有重定向的錯誤頁面,以便嘗試請求/doesntexist將返回404並顯示錯誤頁面。

我嘗試的另一件事是使用app.UseStatusCodePagesWithReExecute("/error/{0}");這確實返回404不改變URL,但只顯示一個空白頁,而不是我的錯誤頁面

+1

像你所添加的中間件以相反的順序管線。 MVC應該始終是最後一個,因爲它是一個「終端」中間件,這意味着一個請求永遠不會通過它。 – khellang

+0

未處理的請求(與MVC中的任何給定路由不匹配)肯定會直通。如果它是終端,那麼它不會被重定向到'/ error'動作 –

+0

是的,你是對的。它*通過未處理的路由:https://github.com/aspnet/Routing/blob/02c92c6f929f1241683a3bb8bad027237f2f679c/src/Microsoft.AspNetCore.Routing/RouterMiddleware.cs#L39。你的中間件的順序仍然不正確。 UseStatusCodePages MW應該總是在UseMvc MW之前。 – khellang

回答

9

在我的應用程序,我做它像這樣:

// custom 404 and error page - this preserves the status code (ie 404) 
app.UseStatusCodePagesWithReExecute("/Home/Error/{0}"); 

我的HomeController有這種操作方法

public IActionResult Error(int statusCode) 
{ 
    if (statusCode == 404) 
    { 
     var statusFeature = HttpContext.Features.Get<IStatusCodeReExecuteFeature>(); 
     if (statusFeature != null) 
     { 
      log.LogWarning("handled 404 for url: {OriginalPath}", statusFeature.OriginalPath); 
     } 

    } 
    return View(statusCode); 
} 

和我的看法是這樣的:

由@khellang提到
@model int 

@{ 
    switch (Model) 
    { 
     case 400: 
      ViewData["Icon"] = "fa fa-ban text-danger"; 
      ViewData["Title"] = "Bad Request"; 
      ViewData["Description"] = "Your browser sent a request that this server could not understand."; 
      break; 
     case 401: 
      ViewData["Icon"] = "fa fa-ban text-danger"; 
      ViewData["Title"] = "Unauthorized"; 
      ViewData["Description"] = "Sorry, but the page requires authentication."; 
     break; 
     case 403: 
      ViewData["Icon"] = "fa fa-exclamation-circle text-danger"; 
      ViewData["Title"] = "Forbidden"; 
      ViewData["Description"] = "Sorry, but you don't have permission to access this page."; 
     break; 
     case 404: 
      ViewData["Icon"] = "fa fa-exclamation-circle text-danger"; 
      ViewData["Title"] = "Page Not Found"; 
      ViewData["Description"] = "Sorry, but the page you were looking for can't be found."; 
      break; 
     case 500: 
     default: 
      ViewData["Icon"] = "fa fa-exclamation-circle text-danger"; 
      ViewData["Title"] = "Unexpected Error"; 
      ViewData["Description"] = "Well, this is embarrassing. An error occurred while processing your request. Rest assured, this problem has been logged and hamsters have been released to fix the problem."; 
      break; 
    } 
} 

<div class="jumbotron text-center"> 
    <header> 
     <h1><span aria-hidden="true" class="@ViewData["Icon"]"></span> @ViewData["Title"]</h1> 
    </header> 
    <p>@ViewData["Description"]</p> 
    <a class="btn btn-primary btn-lg" href="@Url.RouteUrl("/")"><span aria-hidden="true" class="fa fa-home"></span> Site Home</a> 
</div> 

,中間件的順序是非常重要的,這應該是它看起來app.UseMvc

+0

謝謝,這基本上是我的,但你和@ khellang建議這是中間件的順序是搞砸了向上。歡呼 –

+0

謝謝,我不知道StatusFeature!很有幫助! – Dave