• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

asp.netcore错误页面处理【转载】

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

Introduction

Okay, so you’ve probably come across tons of broken links on our enormous world wide web, and it gets even more frustrating when there aren’t any links or buttons redirecting to a working page. However, the hard truth is, in reality, errors are inevitable. Whether they occur through wrong user input, or simply just one or two bugs in your code, they are simply just inevitable.

This article explains perhaps the most efficient way to set up custom error pages for your ASP.NET core web Application. Using custom error pages is immensely satisfying, as opposed to having blank pages, default browser-specific pages, or using the default error pages provided by ASP.NET core.

 

How it works

So, let’s say we run our application and go to a page which doesn’t exist. Once a request gets all the way through to the end of our MVC pipeline, and we have no middleware handling the request, we get the dreaded 404 error (or “Page not found”, if you will). Some browsers might be nice enough to display an error page, others will just leave a blank page, leaving your user slightly nonplussed – Not exactly the kind of thing that makes great user experiences great, is it?.

 

Setting up our custom error page

 

Creating the Controller class

To Begin setting up our error page, we need to create a controller to respond to the request. To keep this example as simple as possible, I'm going to create a simple MVC controller class – as a matter of fact, I will be using the default template provided by visual studio (2015), and then adding a URI template for the route.   

 

 
 
 
 
 
1
public class StatusCodeController : Controller
2
{
3
        // GET: /<controller>/
4
        [HttpGet("/StatusCode/{statusCode}")]
5
        public IActionResult Index(int statusCode)
6
        {
7
            return View(statusCode);
8
        }
9
}
10
 
 

for a more detailed example, we can go all the way to implement logging, using the ILogger  and IStatusCodeReExecuteFeature interfaces, logging the url that the request came through.

 
 
 
 
 
 
1
public class StatusCodeController : Controller
2
{
3
  private readonly ILogger<HomeController> _logger;
4
  public StatusCodeController(ILogger<HomeController> logger)
5
  {
6
    _logger = logger;
7
  }
8
  // GET: /<controller>/
9
  [HttpGet("/StatusCode/{statusCode}")]
10
  public IActionResult Index(int statusCode)
11
  {
12
    var reExecute = HttpContext.Features.Get<IStatusCodeReExecuteFeature>();
13
    _logger.LogInformation($"Unexpected Status Code: {statusCode}, OriginalPath: {reExecute.OriginalPath}");
14
    return View(statusCode);
15
  }
16
}
 
 

 

Creating the view

Next we create the view for the index action of our StatusCode Controller class. 

 

For a simple view, we could have.

 
 
 
 
 
1
@model int
2
<div class="jumbotron">
3
    <h1>Error @Model</h1>
4
    <h3>That’s odd... Something we didn't expect happened"</h3>
5
</div>
 
 

For a more dynamic view, we could go a bit further.

 
 
 
 
 
1
@model int
2
@{
3
    var statusCode = Model;
4
    var statusmessage = "";
5
    switch (statusCode)
6
    {
7
        case 400:
8
            statusmessage = "Bad request: The request cannot be fulfilled due to bad syntax";
9
            break;
10
        case 403:
11
            statusmessage = "Forbidden";
12
            break;
13
        case 404:
14
            statusmessage = "Page not found";
15
            break;
16
        case 408:
17
            statusmessage = "The server timed out waiting for the request";
18
            break;
19
        case 500:
20
            statusmessage = "Internal Server Error - server was unable to finish processing the request";
21
            break;
22
        default:
23
            statusmessage = "That’s odd... Something we didn't expect happened";
24
            break;
25
    }
26
}
27
<div class="jumbotron">
28
    <h1>Error @Model</h1>
29
    <h3>@statusmessage</h3>
30
</div>
 
 

you get the idea. Feel free to include more Http status code cases.

   

Setting up the middleware

Once we are done with the controller and the view, we finish up by setting up a middleware to handle the request in the pipeline, in the configure method of our startup class. Like so.

 
 
 
 
 
1
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
2
{
3
  loggerFactory.AddConsole(Configuration.GetSection("Logging"));
4
  loggerFactory.AddDebug();
5
  if (env.IsDevelopment())
6
  {
7
    app.UseDeveloperExceptionPage();
8
    app.UseBrowserLink();
9
  }
10
  else
11
  {
12
    app.UseExceptionHandler("/Home/Error");
13
  }
14
  app.UseStatusCodePagesWithReExecute("/StatusCode/{0}");
15
  app.UseStaticFiles();
16
  app.UseMvc(routes =>
17
             {
18
    routes.MapRoute(
19
      name: "default",
20
      template: "{controller=Home}/{action=Index}/{id?}");
21
  });
22
}
 
 

And you're done!

 

Results

You can test your error pages by generating errors yourself. Easily begin by navigating to a non-existent page, to try out a 404 error like so. 

The entire code sample used can be found on my github here . Good luck !


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Asp.net(C#)实现验证码功能发布时间:2022-07-10
下一篇:
ASP.NET中使用System.Net.Mail发邮件发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap