2017-05-29 52 views
0

我目前正在使用ASP.NET Core,如何爲未處理的異常設置404默認頁面或NotFound()返回NotFound()或未處理的異常與404頁面

IActionResult Foo() 
{ 
    throw new Exception("Message!"); 
} 

IActionResult Bar() 
{ 
    return NotFound("Message!"); 
} 

我覺得有一個IApplicationBuilder.UseExceptionHandler方法來設置一個錯誤頁面,但我不知道如何配置它。

+0

請點擊這裏https://stackoverflow.com/questions/37690114/asp-net-core-how-to-return-a-specific -status-code-and-no-contents-from-control –

+0

@CarlosFerreira這是一個不同的問題。 – MiP

回答

2

這是explained here

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

你需要一個ErrorController這可能看起來像配置狀態代碼頁

public class ErrorController : Controller 
{ 
    public IActionResult Index(string errorCode) 
    { 
     return View(errorCode); 
    } 
} 

你的觀點(在錯誤的文件夾),將需要被稱爲:

  • 500.csht毫升
  • 404.cshtml

...等

+0

我做這些頁面後,如果我'返回NotFound(「MyError!」)'如何捕獲錯誤字符串? – MiP

+1

使用這種方法,你不能......它只是基於狀態碼重定向 – Alex