ASP.NET 5/6 MVC相当于HttpException的

编程入门 行业动态 更新时间:2024-10-20 05:29:53
本文介绍了ASP.NET 5/6 MVC相当于HttpException的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在MVC 5,你可以抛出一个HttpException与HTTP code,这将设置像这样的反应:

In 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 5 / MVC 6.存在什么是相当于code?

HttpException does not exist in ASP.NET 5 / MVC 6. What is the equivalent code?

推荐答案

一个短暂的聊天@davidfowl ,似乎ASP.NET 5具有 HttpException 或的Htt presponseException 没有这样的概念, 神奇转向响应消息。

After a brief chat with @davidfowl, it seems that ASP.NET 5 has no such notion of HttpException or HttpResponseException that "magically" turn to response messages.

你可以做的,就是挂钩到通过中间件中的ASP.NET 5管道,并建立一个处理异常你。

What you can do, is hook into the ASP.NET 5 pipeline via MiddleWare, and create one that handles the exceptions for you.

下面是从来源$ C ​​$ C自己的错误处理中间件将设置响应状态code 500在异常的进一步上涨管道:

Here is an example from the source code of their error handler middleware which will set the response status code to 500 in case of an exception further up the pipeline:

public class ErrorHandlerMiddleware { private readonly RequestDelegate _next; private readonly ErrorHandlerOptions _options; private readonly ILogger _logger; public ErrorHandlerMiddleware(RequestDelegate next, ILoggerFactory loggerFactory, ErrorHandlerOptions options) { _next = next; _options = options; _logger = loggerFactory.CreateLogger<ErrorHandlerMiddleware>(); if (_options.ErrorHandler == null) { _options.ErrorHandler = _next; } } public async Task Invoke(HttpContext context) { try { await _next(context); } catch (Exception ex) { _logger.LogError("An unhandled exception has occurred: " + ex.Message, ex); if (context.Response.HasStarted) { _logger.LogWarning("The response has already started, the error handler will not be executed."); throw; } PathString originalPath = context.Request.Path; if (_options.ErrorHandlingPath.HasValue) { context.Request.Path = _options.ErrorHandlingPath; } try { var errorHandlerFeature = new ErrorHandlerFeature() { Error = ex, }; context.SetFeature<IErrorHandlerFeature>(errorHandlerFeature); context.Response.StatusCode = 500; context.Response.Headers.Clear(); await _options.ErrorHandler(context); return; } catch (Exception ex2) { _logger.LogError("An exception was thrown attempting to execute the error handler.", ex2); } finally { context.Request.Path = originalPath; } throw; // Re-throw the original if we couldn't handle it } } }

和你需要用它注册 StartUp.cs :

public class Startup { public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerfactory) { app.UseMiddleWare<ExceptionHandlerMiddleware>(); } }

更多推荐

ASP.NET 5/6 MVC相当于HttpException的

本文发布于:2023-11-03 15:26:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555445.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:NET   ASP   HttpException   MVC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!