[英]ASP.NET MVC 6 handling errors based on HTTP status code
我想为每个状态代码显示不同的错误消息,例如:
如何在新的 ASP.NET MVC 6 应用程序中实现这一点? 我可以使用内置的 UseErrorHandler 方法来做到这一点吗?
application.UseErrorHandler("/error");
此外,我注意到即使使用上述处理程序,输入不存在的 URL(例如 /this-page-does-not-exist)也会导致 IIS 出现丑陋的 404 Not Found 错误页面。 这也能怎么处理?
在MVC 5,我们不得不使用ASP.NET System.Web程序的customErrors部分,并在web.config文件中的system.webServer httpErrors部分,但它是很难的工作与一个笨拙的,有很多很奇怪的行为。 MVC 6 是否让这更简单?
你可以使用StatusCodePagesMiddleware
。 以下是一个例子:
public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
{
app.UseStatusCodePagesWithReExecute("/StatusCodes/StatusCode{0}");
app.UseMvcWithDefaultRoute();
处理状态代码请求的控制器:
public class StatusCodesController : Controller
{
public IActionResult StatusCode404()
{
return View(viewName: "NotFound"); // you have a view called NotFound.cshtml
}
... more actions here to handle other status codes
}
一些说明:
UseStatusCodePagesWithRedirects
和UseStatusCodePages
以获取其他功能。 如何在新的ASP.NET MVC 6应用程序中实现这一目标? 我可以使用内置的UseErrorHandler方法执行此操作吗?
快速回答:不是优雅的方式。
说明/替代方法:首先看看UseErrorHandler
方法实际上在做什么: https : //github.com/aspnet/Diagnostics/blob/6dbbe831c493e6e7259de81f83a04d1654170137/src/Microsoft.AspNet.Diagnostics/ErrorHandlerExtensions.cs#L25以下中间件: https : //github.com/aspnet/Diagnostics/blob/6dbbe831c493e6e7259de81f83a04d1654170137/src/Microsoft.AspNet.Diagnostics/ErrorHandlerMiddleware.cs 注意第29-78行(调用方法)
每当请求进入时,都会执行invoke方法(由您的Startup.cs
中的application.UseErrorHandler("...")
的位置控制)。 因此, UseErrorHandler
是添加自定义中间件的一种美化方式:中间件=可以作用于http请求的组件。
现在有了这样的背景,如果我们想要添加我们自己的差异化请求的错误中间件。 我们可以通过修改这些行来添加类似于默认ErrorHandlerMiddleware
的类似中间件来实现这一点: https : //github.com/aspnet/Diagnostics/blob/6dbbe831c493e6e7259de81f83a04d1654170137/src/Microsoft.AspNet.Diagnostics/ErrorHandlerMiddleware.cs#L48-L51通过这种方法,我们可以根据状态代码控制重定向路径。
在MVC 5中,我们不得不使用ASP.NET的system.web customerrors部分和web.config文件中的system.webServer httpErrors部分,但很难处理笨拙,有很多非常奇怪的行为。 MVC 6能让这更简单吗?
答:肯定会:)。 就像上面的回答一样,修复在于添加中间件。 通过Startup.cs
的IApplicationBuilder
添加简单中间件有一条捷径; 在Configure
方法的末尾,您可以添加以下内容:
app.Run(async (context) =>
{
await context.Response.WriteAsync("Could not handle the request.");
// Nothing else will run after this middleware.
});
这将起作用,因为这意味着您到达了http管道的末尾而没有处理请求(因为它位于Startup.cs
中的Configure
方法的末尾)。 如果你想添加这个中间件(以快速方式)和你之后执行中间件的选项,请按以下步骤操作:
app.Use(async (context, next) =>
{
await context.Response.WriteAsync("Could not handle the request.");
// This ensures that any other middelware added after you runs.
await next();
});
希望这可以帮助!
使用各种状态代码,而无需在控制器中单独指定每个状态代码。
Startup.cs:
public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
{
app.UseStatusCodePagesWithRedirects("/StatusCodes?statusCode={0}");
app.UseMvcWithDefaultRoute();
控制器:
public class StatusCodesController : Controller
{
public IActionResult Index(string statusCode)
{
if(statusCode == null) statusCode = "";
if(statusCode == "404") return View("Error404");
return View("Index",statusCode);
}
public IActionResult Test404() { return StatusCode(404); }
public IActionResult Test500() { return StatusCode(500); }
视图:
@model string
@{ ViewData["Title"] = Model + " Oops!"; }
<style>
.error-template {
padding: 40px 15px;
text-align: center;
}
.error-actions {
margin-bottom: 15px;
margin-top: 15px;
}
.error-actions .btn {
margin-right: 10px;
}
</style>
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="error-template">
<h2>Oops!</h2>
<h2>@Model Error</h2>
<div class="error-details">Sorry, an error has occurred!</div>
<div class="error-actions">
<a class="btn btn-primary btn-lg" href="/"><span class="glyphicon glyphicon-home"></span> Take Me Home </a>
<a class="btn btn-default btn-lg" href="/Home/ContactUs"><span class="glyphicon glyphicon-envelope"></span> Contact Support </a>
</div>
</div>
</div>
</div>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.