Ljt*_*Ljt 1 c# asp.net exception asp.net-core
如果发生任何错误,我需要一个全局异常处理程序来处理我的所有控制器方法(我需要向客户端发送一些错误代码)。目前正在每个控制器中编写 try catch 块。下面是我的控制器方法。这很好吗?方法或请建议我使用 asp.net core 3 预览版的解决方案/方法。
[HttpPost]
public ActionResult<Caste> InsertCaste(CasteModel caste)
{
try
{
var result = casteService.InsertCaste(caste);
return CreatedAtAction(nameof(InsertCaste), new { id = result.Id }, result);
}
catch (Exception ex)
{
Log.Logger.log().Error(ex.Message);
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
特尔;博士:
中的一种方法ASP.NET Core
是使用Middlewares
. 在Startup
类和Configure
方法中,添加这段代码:
app.UseExceptionHandler(errorApp =>
{
errorApp.Run(async context =>
{
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
context.Response.ContentType = "application/json";
var contextFeature = context.Features.Get<IExceptionHandlerFeature>();
if(contextFeature != null)
{
await context.Response.WriteAsync(new ExceptionInfo()
{
StatusCode = context.Response.StatusCode,
Message = "Internal Server Error."
}.ToString());
}
});
});
Run Code Online (Sandbox Code Playgroud)
和ExceptionInfo
班级:
public class ExceptionInfo
{
public int StatusCode { get; set; }
public string Message { get; set; }
public override string ToString()
{
return JsonConvert.SerializeObject(this);
}
}
Run Code Online (Sandbox Code Playgroud)
更新 1:
中间件的顺序非常重要,你必须把它放在任何其他中间件之前,例如mvc
:
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseExceptionHandler(... like code above);
app.UseHttpsRedirection();
app.UseMvc();
Run Code Online (Sandbox Code Playgroud)
更新 2:
如果记录您的异常,您可以通过将其类型添加到类中的Configure
方法来注入您的记录器Startup
:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILogger logger)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseExceptionHandler(... like code above);
app.UseHttpsRedirection();
app.UseMvc();
}
Run Code Online (Sandbox Code Playgroud)
更新 3:
使用自定义中间件作为全局异常处理程序:
public class CustomErrorHandlerMiddleware
{
private readonly RequestDelegate _next;
private readonly ILoggerManager _logger;
public ExceptionMiddleware(RequestDelegate next, Ilogger logger)
{
_logger = logger;
_next = next;
}
public async Task InvokeAsync(HttpContext httpContext)
{
try
{
await _next(httpContext);
}
catch (Exception ex)
{
_logger.LogError($"Something went wrong: {ex}");
await HandleExceptionAsync(httpContext, ex);
}
}
private static Task HandleExceptionAsync(HttpContext context, Exception exception)
{
context.Response.ContentType = "application/json";
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
return context.Response.WriteAsync(new ExceptionInfo()
{
StatusCode = context.Response.StatusCode,
Message = "Internal Server Error"
}.ToString());
}
}
Run Code Online (Sandbox Code Playgroud)
然后在Configure
方法中使用它:
app.UseMiddleware<CustomErrorHandlerMiddleware>();
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4315 次 |
最近记录: |