Error handling ASP.NET-CORE-MVC/2自定义错误页,每个页只负责一个指定错误

Error handling ASP.NET-CORE-MVC/2自定义错误页,每个页只负责一个指定错误,error-handling,asp.net-core-mvc,Error Handling,Asp.net Core Mvc,我使用app.usedeveloperceptionpage()以显示我的网站可能存在的任何自定义异常。现在,我的目标是创建一个只针对我指定的单个错误的自定义错误页面 例如,假设我在HomeController.cs中写了一些东西,故意给我一个例外。我只想为那个特定的错误创建一个自定义错误页面,其余的错误由app.UseDeveloperExceptionPage()处理像往常一样 现在基本上我想对两个不同页面上的两个特定错误这样做,所以我需要制作两个自定义错误页面,每个页面负责我选择的一个特定

我使用
app.usedeveloperceptionpage()Startup.cs中编码>以显示我的网站可能存在的任何自定义异常。现在,我的目标是创建一个只针对我指定的单个错误的自定义错误页面

例如,假设我在HomeController.cs中写了一些东西,故意给我一个例外。我只想为那个特定的错误创建一个自定义错误页面,其余的错误由
app.UseDeveloperExceptionPage()处理像往常一样

现在基本上我想对两个不同页面上的两个特定错误这样做,所以我需要制作两个自定义错误页面,每个页面负责我选择的一个特定错误


我怎样才能做到这一点?差不多就是这样,如果我需要提供更多信息,请告诉我。

您只需使用这样的一个视图即可实现 首先,您需要定义以让.NETCore知道如何处理错误

app.UseExceptionHandler("/Error/500");
app.Use(async (context, next) =>
   {
      await next();
      if (context.Response.StatusCode == 404)
      {
         context.Request.Path = "/Error/404";
         await next();
      }
  });
所以基本上我将处理500404错误并将其重定向到我的错误控制器

然后创建错误控制器

    public class ErrorController: Controller
    {
        private readonly IExceptionHandler _exceptionHandler;
        public ErrorController(IExceptionHandler exceptionHandler)
        {
            _exceptionHandler = exceptionHandler;
        }

        [HttpGet("/Error/{statusCode}")]
        public async Task<IActionResult> Index(int statusCode)
        {
            if (statusCode == (int)HttpStatusCode.InternalServerError)
            {
                var exceptionFeature = HttpContext.Features.Get<IExceptionHandlerPathFeature>();
                var exceptionThatOccurred = exceptionFeature.Error;
                await _exceptionHandler.HandleExceptionAsync(exceptionThatOccurred);
            }
            return View(statusCode);
        }
    }
公共类错误控制器:控制器
{
私有只读IEExceptionHandler _exceptionHandler;
公共错误控制器(IEExceptionHandler exceptionHandler)
{
_exceptionHandler=exceptionHandler;
}
[HttpGet(“/Error/{statusCode}”)]
公共异步任务索引(int statusCode)
{
如果(statusCode==(int)HttpStatusCode.InternalServerError)
{
var exceptionFeature=HttpContext.Features.Get();
var exceptionatoccurred=exceptionFeature.Error;
Wait _exceptionHandler.HandleExceptionSync(ExceptionAtOccurred);
}
返回视图(状态代码);
}
}
最后

@{
    ViewData["Title"] = Model;
}
@model int
@{
    var statusCode = Model;
    var statusmessage = "";
    switch (statusCode)
    {
        case 400:
            statusmessage = "Bad request: The request cannot be fulfilled due to bad syntax";
            break;
        case 403:
            statusmessage = "Forbidden: You are not authorize to use system. Please contact admin for detail";
            break;
        case 404:
            statusmessage = "Page not found";
            break;
        case 408:
            statusmessage = "The server timed out waiting for the request";
            break;
        case 500:
            statusmessage = "Internal Server Error";
            break;
        default:
            statusmessage = "That’s odd... Something we didn't expect happened";
            break;
    }
}

<div class="error-page-wrapper row2">
    <div id="error-container" class="clear">
        <div id="error-page" class="clear">
            <div class="hgroup">
                <h1>@statusmessage</h1>
                <h2>@Model Error</h2>
            </div>
            <p>Go <a href="javascript:history.go(-1)">Back</a> or <a href="/">Home</a></p>
        </div>
    </div>
</div>
@{
ViewData[“Title”]=模型;
}
@模型int
@{
var statusCode=模型;
var statusmessage=“”;
开关(状态代码)
{
案例400:
statusmessage=“错误请求:由于语法错误,无法完成请求”;
打破
案例403:
statusmessage=“禁止:您无权使用系统。请联系管理员了解详细信息”;
打破
案例404:
statusmessage=“未找到页面”;
打破
案例408:
statusmessage=“服务器在等待请求时超时”;
打破
案例500:
statusmessage=“内部服务器错误”;
打破
违约:
statusmessage=“这很奇怪……发生了我们意想不到的事情”;
打破
}
}
@状态信息
@模型误差
去还是去

所以你可以看到if子句处理500个错误,如果你不想处理500个错误,你可以删除它


添加:
app.UseExceptionHandler(“/Error/500”);使用(异步(上下文,下一步)=>{await next();if(context.Response.StatusCode==404){context.Request.Path=“/Error/404”;await next();})在Startup.cs中,创建ErrorController时,会出现以下错误: