C# 全局异常处理程序-使用数据重定向
我想问一下如何将异常处理程序重定向到C# 全局异常处理程序-使用数据重定向,c#,asp.net-core,asp.net-core-mvc,C#,Asp.net Core,Asp.net Core Mvc,我想问一下如何将异常处理程序重定向到Error控制器的ActionErrorPage,并将异常消息作为该方法的参数 到目前为止,我尝试了这些,但控制器方法中的字符串为null app.UseExceptionHandler( options => { options.Run( async httpcontext => { httpcontext.Response.ContentType = "text/html";
Error
控制器的ActionErrorPage
,并将异常消息作为该方法的参数
到目前为止,我尝试了这些,但控制器方法中的字符串为null
app.UseExceptionHandler(
options =>
{
options.Run(
async httpcontext =>
{
httpcontext.Response.ContentType = "text/html";
var ex = httpcontext.Features.Get<IExceptionHandlerFeature>();
if (ex != null)
{
httpcontext.Items.Add.("error", ex.Error.Message);
httpcontext.Response.Redirect("/Error/ErrorPage/");
}
});
});
还有这个:
public IActionResult ErrorPage(string error)
{
// other logic
return View("ExceptionView", error);
}
我也试过这样做:
app.UseExceptionHandler(
options =>
{
options.Run(
async httpcontext =>
{
httpcontext.Response.ContentType = "text/html";
var ex = httpcontext.Features.Get<IExceptionHandlerFeature>();
if (ex != null)
{
//httpcontext.Request.Path = "/Error/ErrorPage/";
//httpcontext.Request.QueryString = new QueryString($"?error={ex.Error.Message}");
await httpcontext.Response.WriteAsync(JsonConvert.SerializeObject(new
{
error = ex.Error.Message
}));
httpcontext.Response.Redirect("/Error/ErrorPage/");
}
});
});
这就是整个控制器
public class Error : ControllerBase
{
public Error(Context context)
{
_context = context;
}
public IActionResult ErrorPage([FromBody] string error)
{
return View("ExceptionView", error);
}
}
以下是我们的设置:
Startup.cs
public virtual void Configure(
IApplicationBuilder app,
IHostingEnvironment env,
ILoggerFactory loggerFactory,
IApplicationLifetime lifetime)
{
...
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Errors");
}
...
}
ErrorsController.cs:
public class ErrorsController : Controller
{
private readonly ILogger<ErrorController> _logger;
private readonly IHostingEnvironment _environment;
private readonly IHttpContextAccessor _httpContext;
private readonly JiraSettings _jiraSettings;
public ErrorController(
ILogger<ErrorController> logger,
IHostingEnvironment environment,
IHttpContextAccessor httpContext)
{
_logger = logger;
_environment = environment;
_httpContext = httpContext;
}
[HttpGet]
public IActionResult Get()
{
var exception = HttpContext.Features.Get<IExceptionHandlerFeature>();
if (exception?.Error == null)
{
return NoContent();
}
var errorMessage = $"An error occurred while processing your request {exception.Error.GetErrorId()}";
if (_environment.IsProduction() || _environment.IsStaging())
{
return Json(errorMessage);
}
errorMessage += $"\n{exception.Error.StackTrace}";
return Json(errorMessage);
}
公共类错误控制器:控制器
{
专用只读ILogger\u记录器;
私有只读IHostingEnvironment\u环境;
专用只读IHttpContextAccessor\u httpContext;
私人只读JiraSettings_JiraSettings;
公共差错控制器(
ILogger记录器,
我熟悉环境,
IHttpContextAccessor(httpContext)
{
_记录器=记录器;
_环境=环境;
_httpContext=httpContext;
}
[HttpGet]
public IActionResult Get()
{
var exception=HttpContext.Features.Get();
if(异常?.Error==null)
{
返回NoContent();
}
var errorMessage=$“处理请求时发生错误{exception.error.GetErrorId()}”;
if(_environment.IsProduction()|||u environment.IsStaging())
{
返回Json(errorMessage);
}
errorMessage+=$“\n{exception.Error.StackTrace}”;
返回Json(errorMessage);
}
神奇的是
IEExceptionHandlerFeature
,它让您可以访问原始异常对象。正如您所看到的,我们将错误输出为字符串,因为我们正在执行一个javascript接收错误消息字符串的角度应用程序。您可以改为创建一个视图模型并返回一个视图。我们不显示异常信息在生产或登台环境中。GetErrorId()
是我们编写的一个扩展方法,它可以为异常获取唯一的id。错误id会显示给用户,并写入日志(其他地方)。解决问题的运气如何?@mortb还没有,但我仍然要从下面尝试这个答案
public virtual void Configure(
IApplicationBuilder app,
IHostingEnvironment env,
ILoggerFactory loggerFactory,
IApplicationLifetime lifetime)
{
...
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Errors");
}
...
}
public class ErrorsController : Controller
{
private readonly ILogger<ErrorController> _logger;
private readonly IHostingEnvironment _environment;
private readonly IHttpContextAccessor _httpContext;
private readonly JiraSettings _jiraSettings;
public ErrorController(
ILogger<ErrorController> logger,
IHostingEnvironment environment,
IHttpContextAccessor httpContext)
{
_logger = logger;
_environment = environment;
_httpContext = httpContext;
}
[HttpGet]
public IActionResult Get()
{
var exception = HttpContext.Features.Get<IExceptionHandlerFeature>();
if (exception?.Error == null)
{
return NoContent();
}
var errorMessage = $"An error occurred while processing your request {exception.Error.GetErrorId()}";
if (_environment.IsProduction() || _environment.IsStaging())
{
return Json(errorMessage);
}
errorMessage += $"\n{exception.Error.StackTrace}";
return Json(errorMessage);
}