学习Net Core 2.0 做博客 错误页中间件
2021-05-15 22:29
标签:star and war request ext while 中间 div std CustomErrorPagesMiddleware ErrorPage CustomErrorPagesExtensions Startup 学习Net Core 2.0 做博客 错误页中间件 标签:star and war request ext while 中间 div std 原文地址:http://www.cnblogs.com/wyzy/p/7749908.htmlpublic class CustomErrorPagesMiddleware
{
private readonly RequestDelegate _next;
private readonly Microsoft.Extensions.Logging.ILogger _logger;
private readonly IHostingEnvironment _env;
public CustomErrorPagesMiddleware(
RequestDelegate next,
ILoggerFactory loggerFactory,
IHostingEnvironment env)
{
_next = next;
_logger = loggerFactory.CreateLogger
public static class ErrorPage
{
public static async Task ResponseAsync(HttpResponse response, int statusCode, IHostingEnvironment env)
{
if (statusCode == 404)
{
await response.WriteAsync(Page404,Encoding.UTF8);
}
else if (statusCode == 500)
{
await response.WriteAsync(Page500);
}else if (statusCode == 403)
{
//没有权限服务器会返回403;
await response.WriteAsync(Page403);
}
}
private static string Page404 => "404";
private static string Page500 => "500";
private static string Page403 => "403";
}
public static class CustomErrorPagesExtensions
{
public static IApplicationBuilder UseCustomErrorPages(this IApplicationBuilder app)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
return app.UseMiddleware
app.UseCustomErrorPages();
上一篇:web网站的基本分类
下一篇:URL的应用
文章标题:学习Net Core 2.0 做博客 错误页中间件
文章链接:http://soscw.com/index.php/essay/85981.html