ASP.NET Core 等效于 ASP.NET MVC 5 的 HttpException

ASP.NET Core equivalent of ASP.NET MVC 5#39;s HttpException(ASP.NET Core 等效于 ASP.NET MVC 5 的 HttpException)

本文介绍了ASP.NET Core 等效于 ASP.NET MVC 5 的 HttpException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 ASP.NET MVC 5 中,您可以抛出 HttpException 带有 HTTP 代码,这将设置响应如下:

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.BadRequest, "Bad Request.");

HttpException 在 ASP.NET Core 中不存在.什么是等效代码?

HttpException does not exist in ASP.NET Core. What is the equivalent code?

推荐答案

我实现了自己的 HttpException 并支持中间件,它可以捕获所有 HttpException 并将它们转换为相应的错误响应.下面可以看到一个简短的摘录.您还可以使用 Boxed.AspNetCore Nuget 包.

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.

public void Configure(IApplicationBuilder application)
{
    application.UseIISPlatformHandler();

    application.UseStatusCodePagesWithReExecute("/error/{0}");
    application.UseHttpException();

    application.UseMvc();
}

扩展方法

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

中间件

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<IHttpResponseFeature>();
            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.

这篇关于ASP.NET Core 等效于 ASP.NET MVC 5 的 HttpException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:ASP.NET Core 等效于 ASP.NET MVC 5 的 HttpException

基础教程推荐