zoukankan      html  css  js  c++  java
  • 处理 ASP.NET Core 中的错误

    官方文档

    https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/error-handling?view=aspnetcore-5.0

    开发异常页面

    app.UseDeveloperExceptionPage();

    效果:

    自定义一个开发者页面

    if (env.IsDevelopment())
    {
        //app.UseDeveloperExceptionPage();
        app.UseExceptionHandler("/Home/DevelopmentError");
    }

    控制器:

     public IActionResult DevelopmentError()
     {
         var exception = HttpContext.Features.Get<IExceptionHandlerPathFeature>();
         ViewBag.ExcetionPath = exception.Path;//异常路径
         ViewBag.ExcetionMessage = exception.Error.Message;//异常内容
         ViewBag.ExcetionStackTrace = exception.Error.StackTrace;//异常堆栈
         return View();
     }

    视图:

    @{
        ViewData["Title"] = "Development Error";
    }
    <h1>错误详细:</h1>
    <div class="alert alert-danger">
        <h5>异常路径:</h5>
        <hr />
        <p>@ViewBag.ExcetionPath</p>
    </div>
    <div class="alert alert-danger">
        <h5>异常内容:</h5>
        <hr />
        <p>@ViewBag.ExcetionMessage</p>
    </div>
    <div class="alert alert-danger">
        <h5>异常堆栈:</h5>
        <hr />
        <p>@ViewBag.ExcetionStackTrace</p>
    </div>

    效果:

    异常处理程序页

     app.UseExceptionHandler("/Home/Error");

    在HomeController下自定义Error

    效果:

    状态代码页

    //当请求的地址不存在时,返回的页面
    app.UseStatusCodePages();

     效果:

     状态码和重定向页

    当我输入http://localhost:52062/Home/aa时,该页面首先向客户端发送“302 - 已找到”状态代码
    其次重定向到 URL 模板中提供的错误处理终结点(http://localhost:52062/Home/Error),并且返回200状态码。
    app.UseStatusCodePagesWithRedirects("/Home/Error");

    效果:

     状态码页重新执行

     向客户端返回原始状态代码,错误是怎么样就返回什么状态码。

     通过使用备用路径重新执行请求管道,从而生成响应正文

     app.UseStatusCodePagesWithReExecute("/Home/Error");

    效果:

  • 相关阅读:
    android之Toast多次提示延时处理
    android之双击返回键退出程序
    android之对话框“确定退出吗?”
    数组随机排序
    android全屏显示,去掉标题栏和信息栏
    android四大组件之service生命周期
    android四大组件之activity生命周期
    struct和union,enum分析
    const和volatile分析
    goto,void,extern,sizeof分析
  • 原文地址:https://www.cnblogs.com/-zzc/p/14644634.html
Copyright © 2011-2022 走看看