问题描述
<一个href=\"http://stackoverflow.com/questions/10660721/what-is-the-difference-between-htt$p$psponsemessage-and-htt$p$psponseexception\">Here @GlennBlock提到扔的Htt presponseException
立即停止执行请求而返回的Htt presponseMessage
不会停止要求传播(我的理解它)。
Here @GlennBlock mentioned that throwing HttpResponseException
immediately stops request execution while returning HttpResponseMessage
does not stop request propagation (as I understood it).
唯一的例外是有用的,立即停止处理并退出。
如果不是我回到了Htt presponseMessage,请求将继续愉快其余它的处理,并返回一个404的主要区别是结束请求或没有。
If instead I return HttpResponseMessage, the request will happily continue the rest of it's processing and return a 404. The main difference being end the request or not.
在.NET的Web API 2,你也可以从 ApiController
返回 IHttpActionResult
。它要求停止繁殖或它的工作原理类似于的Htt presponseMessage
?
In .NET Web API 2 you can also return IHttpActionResult
from ApiController
. Does it stop request propagation or it works similar to HttpResponseMessage
?
感谢您;)
推荐答案
我觉得@GlennBlock在技术上是错在这里。抛出的Htt presponseException
不会做任何事情比返回显著不同的的Htt presponseMessage
。
I think @GlennBlock is technically wrong here. Throwing an HttpResponseException
does not do anything significantly different than returning an HttpResponseMessage
.
下面是在:
try
{
return await SendAsyncCore(request, cancellationToken);
}
catch (HttpResponseException httpResponseException)
{
return httpResponseException.Response;
}
catch (Exception exception)
{
exceptionInfo = ExceptionDispatchInfo.Capture(exception);
}
在管道中的这一点上,如果你已经抛出的Htt presponseException
,如果你回到它被视为相同的的Htt presponseMessage
......它的行为完全一样的。
At this point in the pipeline if you had thrown an HttpResponseException
it gets treated the same as if you returned an HttpResponseMessage
... it behaves exactly the same.
有一件事你会注意到的是,与Visual Studio调试调试器时将打破在抛出的Htt presponseException
(因为渔获没有在用户$发生C $ C)。如果它是在你的网站的正常运行一般会发生一个预期的错误那么这可以得到真烦人!在这种情况下,你可能反而要与的Htt presponseMessage
返回一个错误code。
One thing you will notice is that when debugging with Visual Studio the debugger will break on a thrown HttpResponseException
(since the catch is not happening in user code). If it's an expected error that happens commonly in normal operation of your site then this can get really annoying! In this case you probably instead want to return an error code with HttpResponseMessage
.
在另一方面,如果它应该从理论上没有,那么你希望由调试器被警告发生一个奇怪的错误。你不想被悄悄地忽略错误。你想知道这个问题,因此理论上可以修复它。在这种情况下抛出的Htt presponseException
更有意义。
On the other hand, if it's a bizarre error which should theoretically not happen then you want to be alerted by the debugger. You don't want the error to be quietly ignored. You want to know about it so you can theoretically fix it. In this case throwing the HttpResponseException
makes more sense.
关于唯一的区别我可以告诉大家的是,抛出一个的Htt presponseException
允许你用抓住它个IExceptionFilter
。类似于您如何应用一个动作过滤器或授权过滤器的方法或控制器,你可以申请一个异常过滤器来处理所有的异常。也许你想登录他们在您的数据库中。这显然是一个更容易应用属性过滤器中,而不是在每一个操作方法写一个try / catch。
About the only other difference I can tell is that throwing an HttpResponseException
allows you to catch it with an IExceptionFilter
. Similar to how you can apply an action filter or an authorization filter to a method or controller, you can apply an exception filter to handle all exceptions. Perhaps you want to log them in your database. It's obviously a lot easier to apply an attribute filter than to have to write a try/catch in every single action method.
在这种情况下,抛出一个异常,实际上做的更多的工作,需要不是简单地返回响应时间。但它是一个区别。
In this case throwing an exception actually does more work and takes longer than simply returning a response. But it is one difference.
这是 IHttpActionResult
被用来创建一个的Htt presponseMessage
。把它看成是一个的Htt presponseMessage
工厂在那里你可以一次写的并由多个action方法使用它。在这种情况下,你可以从内部 IHttpActionResult
扔的Htt presponseException
。这不是不是直接从动作做起来有什么不同。
An IHttpActionResult
is used to create an HttpResponseMessage
. Think of it as a HttpResponseMessage
factory where you can write it once and use it by multiple action methods. In this case you could throw the HttpResponseException
from within the IHttpActionResult
. It's not any different than doing it directly from the action.
这篇关于是否返回IHttpActionResult停止请求执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!