ASP.NET Core Web Api中间件自定义异常

时间:2019-06-28 17:35:44

标签: exception asp.net-core asp.net-core-webapi asp.net-core-middleware

ASP.NET Core Web Api中间件将自定义异常转换为基本异常

我创建了一个自定义的异常类,并使用它引发异常。中间件捕获了它,但是抛出了基本异常而不是自定义异常。我不明白为什么它不能捕获自定义异常。

自定义例外

public class CustomException : Exception
{
    public int HttpStatusCode { get; private set; }

    public CustomException()
    {

    }

    public CustomException(int httpStatusCode, string message):base(message)
    {
        HttpStatusCode = httpStatusCode;
    }
}

中间件

public class ExceptionMiddleware
{
    public readonly RequestDelegate _next;
    public ExceptionMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    public async Task InvokeAsync(HttpContext httpContext)
    {
        try
        {
            await _next(httpContext);
        }
        catch(CustomException ex)
        {
            await HandleExceptionAsync(httpContext, ex);
        }
        catch(Exception ex)
        {
            await HandleExceptionAsync(httpContext, ex);
        }
    }

    private static Task HandleExceptionAsync(HttpContext context, Exception ex)
    {
        int statusCode = (int)HttpStatusCode.InternalServerError;
        if (ex is CustomException)
        {
            CustomException se = ex as CustomException;
            statusCode = se.HttpStatusCode;
        }
        context.Response.ContentType = "application/json";
        context.Response.StatusCode = statusCode;

        return context.Response.WriteAsync(JsonConvert.SerializeObject(new InternalServerErrorResponse(ex.Message)));
    }
}

引发异常

throw new CustomException(StatusCodes.Status422UnprocessableEntity, "User is already registered!");

引发异常时,中间件不会捕获自定义异常,而是基本Exception。它总是转到下面的代码块

catch(Exception ex)
{
     await HandleExceptionAsync(httpContext, ex);
}

请告知。

2 个答案:

答案 0 :(得分:0)

您必须像这样告诉您的应用在middleware类的configure方法中以startup方法使用此public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerManager logger){ app.ExceptionMiddleware(); } -

{{1}}

答案 1 :(得分:0)

您可以使用异常过滤器来处理异常并轻松操纵响应。

public class ExceptionFilter : IExceptionFilter
{


    public ExceptionFilter(IHostingEnvironment env, ILogger<JsonExceptionFilter> logger)
    {
        _env = env;
        _logger = logger;
    }

    public void OnException(ExceptionContext context)
    {
        var error = new ApiResponse();
        var exceptionName = context.Exception.GetType().Name;
        var message = context.Exception.Message;

        if (_env.IsDevelopment())
        {
            error.Message = context.Exception.Message;
            error.Detail = context.Exception.StackTrace;  
        }
        else
        {
           //Prevent To Show Exception Messages On Production
            error.Message = "Server Error Occured";
            error.Detail = "Something wrong happened";  
        }

        context.Result = new ObjectResult(error)
        {
            //Manipulate Status Code
            StatusCode = 500
        };
    }

}

并注册到Startup.cs

public void ConfigureServices(IServiceCollection services){

        services.AddMvc(options =>
        {
            //Catch the exceptions
            options.Filters.Add<ExceptionFilter>();
        }).SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

 }