ASP.NET MVC 6基于HTTP状态代码处理错误
我想为每个状态代码显示不同的错误消息,例如:
如何在新的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 }
一些说明:
如何在新的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行(调用方法)
每当请求进入时(由您的Startup.cs
中的application.UseErrorHandler("...")
的位置控制)都会执行invoke方法。 因此, 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); }
视图:
上述就是C#学习教程:ASP.NET MVC 6基于HTTP状态代码处理错误分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
@model string @{ ViewData["Title"] = Model + " Oops!"; }
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1004041.html