ASP.NET Core equivalent of ASP.NET MVC 5's HttpException

后端 未结 6 1845
攒了一身酷
攒了一身酷 2020-12-02 11:59

In ASP.NET MVC 5 you could throw a HttpException with a HTTP code and this would set the response like so:

throw new HttpException((int)HttpStatusCode.BadReq         


        
6条回答
  •  难免孤独
    2020-12-02 12:44

    I implemented my own HttpException and supporting middleware which catches all HttpException's and turns them into the corresponding error response. A short extract can be seen below. You can also use the Boxed.AspNetCore Nuget package.

    Usage Example in Startup.cs

    public void Configure(IApplicationBuilder application)
    {
        application.UseIISPlatformHandler();
    
        application.UseStatusCodePagesWithReExecute("/error/{0}");
        application.UseHttpException();
    
        application.UseMvc();
    }
    

    Extension Method

    public static class ApplicationBuilderExtensions
    {
        public static IApplicationBuilder UseHttpException(this IApplicationBuilder application)
        {
            return application.UseMiddleware();
        }
    }
    

    Middleware

    internal class HttpExceptionMiddleware
    {
        private readonly RequestDelegate next;
    
        public HttpExceptionMiddleware(RequestDelegate next)
        {
            this.next = next;
        }
    
        public async Task Invoke(HttpContext context)
        {
            try
            {
                await this.next.Invoke(context);
            }
            catch (HttpException httpException)
            {
                context.Response.StatusCode = httpException.StatusCode;
                var responseFeature = context.Features.Get();
                responseFeature.ReasonPhrase = httpException.Message;
            }
        }
    }
    

    HttpException

    public class HttpException : Exception
    {
        private readonly int httpStatusCode;
    
        public HttpException(int httpStatusCode)
        {
            this.httpStatusCode = httpStatusCode;
        }
    
        public HttpException(HttpStatusCode httpStatusCode)
        {
            this.httpStatusCode = (int)httpStatusCode;
        }
    
        public HttpException(int httpStatusCode, string message) : base(message)
        {
            this.httpStatusCode = httpStatusCode;
        }
    
        public HttpException(HttpStatusCode httpStatusCode, string message) : base(message)
        {
            this.httpStatusCode = (int)httpStatusCode;
        }
    
        public HttpException(int httpStatusCode, string message, Exception inner) : base(message, inner)
        {
            this.httpStatusCode = httpStatusCode;
        }
    
        public HttpException(HttpStatusCode httpStatusCode, string message, Exception inner) : base(message, inner)
        {
            this.httpStatusCode = (int)httpStatusCode;
        }
    
        public int StatusCode { get { return this.httpStatusCode; } }
    }
    

    In the long term, I would advise against using exceptions for returning errors. Exceptions are slower than just returning an error from a method.

提交回复
热议问题