Вы можете создать новое промежуточное программное обеспечение для обработки ваших исключений:
public class ErrorHandlingMiddleware
{
private readonly RequestDelegate _next;
/// <summary>
/// Default constructor
/// </summary>
/// <param name="next">Next request in the pipeline</param>
public ErrorHandlingMiddleware(RequestDelegate next)
{
_next = next;
}
/// <summary>
/// Entry point into middleware logic
/// </summary>
/// <param name="context">Current http context</param>
/// <returns></returns>
public async Task Invoke(HttpContext context)
{
try
{
await _next(context);
}
catch (HttpException httpException)
{
context.Response.StatusCode = httpException.StatusCode;
}
catch (Exception ex)
{
await HandleExceptionAsync(context, ex);
}
}
private static Task HandleExceptionAsync(HttpContext context, Exception exception)
{
var code = HttpStatusCode.InternalServerError; // 500 if unexpected
var result = JsonConvert.SerializeObject(new { Error = "Internal Server error" });
context.Response.ContentType = "application/json";
context.Response.StatusCode = (int)code;
return context.Response.WriteAsync(result);
}
}
Используйте его следующим образом в Startup.cs
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMiddleware(typeof(ErrorHandlingMiddleware));
app.UseMvc();
}