<legend id='clpOW'><style id='clpOW'><dir id='clpOW'><q id='clpOW'></q></dir></style></legend>
  • <i id='clpOW'><tr id='clpOW'><dt id='clpOW'><q id='clpOW'><span id='clpOW'><b id='clpOW'><form id='clpOW'><ins id='clpOW'></ins><ul id='clpOW'></ul><sub id='clpOW'></sub></form><legend id='clpOW'></legend><bdo id='clpOW'><pre id='clpOW'><center id='clpOW'></center></pre></bdo></b><th id='clpOW'></th></span></q></dt></tr></i><div id='clpOW'><tfoot id='clpOW'></tfoot><dl id='clpOW'><fieldset id='clpOW'></fieldset></dl></div>
    <tfoot id='clpOW'></tfoot>

    <small id='clpOW'></small><noframes id='clpOW'>

        <bdo id='clpOW'></bdo><ul id='clpOW'></ul>

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

        时间:2023-06-08

        <tfoot id='AQSFT'></tfoot>
          1. <small id='AQSFT'></small><noframes id='AQSFT'>

              • <bdo id='AQSFT'></bdo><ul id='AQSFT'></ul>

                    <tbody id='AQSFT'></tbody>
                  <i id='AQSFT'><tr id='AQSFT'><dt id='AQSFT'><q id='AQSFT'><span id='AQSFT'><b id='AQSFT'><form id='AQSFT'><ins id='AQSFT'></ins><ul id='AQSFT'></ul><sub id='AQSFT'></sub></form><legend id='AQSFT'></legend><bdo id='AQSFT'><pre id='AQSFT'><center id='AQSFT'></center></pre></bdo></b><th id='AQSFT'></th></span></q></dt></tr></i><div id='AQSFT'><tfoot id='AQSFT'></tfoot><dl id='AQSFT'><fieldset id='AQSFT'></fieldset></dl></div>
                1. <legend id='AQSFT'><style id='AQSFT'><dir id='AQSFT'><q id='AQSFT'></q></dir></style></legend>
                  本文介绍了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 Web API 使用 AddMvcCore() 下一篇:在 ASP.NET Core 1.0 上处理大文件上传

                  相关文章

                  <i id='AXpmQ'><tr id='AXpmQ'><dt id='AXpmQ'><q id='AXpmQ'><span id='AXpmQ'><b id='AXpmQ'><form id='AXpmQ'><ins id='AXpmQ'></ins><ul id='AXpmQ'></ul><sub id='AXpmQ'></sub></form><legend id='AXpmQ'></legend><bdo id='AXpmQ'><pre id='AXpmQ'><center id='AXpmQ'></center></pre></bdo></b><th id='AXpmQ'></th></span></q></dt></tr></i><div id='AXpmQ'><tfoot id='AXpmQ'></tfoot><dl id='AXpmQ'><fieldset id='AXpmQ'></fieldset></dl></div>

                  <small id='AXpmQ'></small><noframes id='AXpmQ'>

                  • <bdo id='AXpmQ'></bdo><ul id='AXpmQ'></ul>

                      <legend id='AXpmQ'><style id='AXpmQ'><dir id='AXpmQ'><q id='AXpmQ'></q></dir></style></legend>

                      <tfoot id='AXpmQ'></tfoot>