相当于 .net Core webapi 2 的 HttpResponseException/IHttpActionResponse(不是 mvc)
Equivalent of HttpResponseException/IHttpActionResponse for .net Core webapi 2 (not mvc)
当我阅读有关响应请求和处理错误的 webapi 时,一切都基于:
IHttpActionResult
HttpResponseException
但是当你创建一个.net core webapi项目时,这些是不可用的。我可以找到 IActionResult,这似乎是等价的?
但我正在兜圈子试图找出一个非常简单的事情,即如何处理 .net 核心 webapi 中的错误,因为 HttpResponseException 不可用。我得到的印象是其中包含 'http' 的所有内容,仅适用于完整的 MVC 应用程序。
我只想return一个错误,一定很简单...
IActionResult
相当于 IHttpActionResult
,如您所建议的。这是 ASP.NET 核心 MVC 中所谓的 MVC 和 Web API 整合的一部分。
至于HttpResponseException
,这已在ASP.NET Core 中完全删除。在 GitHub 上有一个有趣的 issue,David Fowler 解释了为什么会这样:
Just the classic, "we don't want people using exceptions for control flow" paradigm. People did things like use it from within their business logic which is all sorts of wrong. I can't speak to the performance issues because I haven't measured but throwing an exception that is meant to be caught to get some data is worse than just returning that result.
建议的替代方法是使用各种 IActionResult
实现来创建 JSON 响应、返回错误等。同样,来自问题:
My suggestion would be to have the action method return IActionResult (or the async variation).
The reason that both IActionResult and object are supported is that they each suit different purposes and different styles. For some of the simplest cases, it's nice to use object, but it isn't powerful at all. For full control, there's IActionResult, which follows the well-known "command pattern."
The IActionResult pattern allows the controller to explicitly state what should happen as a result of the action: some kind of error code, a redirect, a serialized data object, a rendered view, etc.
如果您希望在控制器之外处理错误,您可能会受益于阅读 docs on this topic, which goes into the details of using either middleware or filters for error-handling. In the comments, there's a link to a tutorial,其中更详细地解释了错误处理的某些方面。
为了完整性,这里是中间件方法教程中的代码:
app.UseExceptionHandler(
options => {
options.Run(
async context =>
{
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
context.Response.ContentType = "text/html";
var ex = context.Features.Get<IExceptionHandlerFeature>();
if (ex != null)
{
var err = $"<h1>Error: {ex.Error.Message}</h1>{ex.Error.StackTrace }";
await context.Response.WriteAsync(err).ConfigureAwait(false);
}
});
}
);
关于 IExceptionFilter
方法还有更多详细信息,但我不会在这里重复所有内容。
当我阅读有关响应请求和处理错误的 webapi 时,一切都基于:
IHttpActionResult
HttpResponseException
但是当你创建一个.net core webapi项目时,这些是不可用的。我可以找到 IActionResult,这似乎是等价的?
但我正在兜圈子试图找出一个非常简单的事情,即如何处理 .net 核心 webapi 中的错误,因为 HttpResponseException 不可用。我得到的印象是其中包含 'http' 的所有内容,仅适用于完整的 MVC 应用程序。
我只想return一个错误,一定很简单...
IActionResult
相当于 IHttpActionResult
,如您所建议的。这是 ASP.NET 核心 MVC 中所谓的 MVC 和 Web API 整合的一部分。
至于HttpResponseException
,这已在ASP.NET Core 中完全删除。在 GitHub 上有一个有趣的 issue,David Fowler 解释了为什么会这样:
Just the classic, "we don't want people using exceptions for control flow" paradigm. People did things like use it from within their business logic which is all sorts of wrong. I can't speak to the performance issues because I haven't measured but throwing an exception that is meant to be caught to get some data is worse than just returning that result.
建议的替代方法是使用各种 IActionResult
实现来创建 JSON 响应、返回错误等。同样,来自问题:
My suggestion would be to have the action method return IActionResult (or the async variation).
The reason that both IActionResult and object are supported is that they each suit different purposes and different styles. For some of the simplest cases, it's nice to use object, but it isn't powerful at all. For full control, there's IActionResult, which follows the well-known "command pattern."
The IActionResult pattern allows the controller to explicitly state what should happen as a result of the action: some kind of error code, a redirect, a serialized data object, a rendered view, etc.
如果您希望在控制器之外处理错误,您可能会受益于阅读 docs on this topic, which goes into the details of using either middleware or filters for error-handling. In the comments, there's a link to a tutorial,其中更详细地解释了错误处理的某些方面。
为了完整性,这里是中间件方法教程中的代码:
app.UseExceptionHandler(
options => {
options.Run(
async context =>
{
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
context.Response.ContentType = "text/html";
var ex = context.Features.Get<IExceptionHandlerFeature>();
if (ex != null)
{
var err = $"<h1>Error: {ex.Error.Message}</h1>{ex.Error.StackTrace }";
await context.Response.WriteAsync(err).ConfigureAwait(false);
}
});
}
);
关于 IExceptionFilter
方法还有更多详细信息,但我不会在这里重复所有内容。