我今天在WebAPI2中构建了一个方法。 并问到如果我应该使我的方法无效或保留IHttpActionResult?
作为优势,我看到IHttpActionResult可以返回任何响应代码。 无效方法的优点是什么? 它是更快还是唯一的区别,它总是响应200(或者是204?)。
有人知道哪一个更快吗? 或者它与我所命名的有什么其他的主要区别? (这包括void无法响应任何内容以及IHttpActionResult可以.......)
I was building a method today in my WebAPI2. and came to the question if i should make my method a void or keep it as IHttpActionResult?
As advantage i see that IHttpActionResult can return any response code. What is the advantage of the void approach? is it faster or is the only difference that it always response 200 (or is it 204?).
Does somebody know which one is faster? or if it has any other major difference than the ones that i have named? (this includes the fact that void can't respond anything and IHttpActionResult can.......)
最满意答案
来自asp.net web api应用程序的每条消息都会返回一个http状态代码。 一个exception是昂贵的,所以,如果你把它传递给web api,框架应该知道如何领导这个并且改变为http message结果。
没有极端的区别。 web api中的void action方法将返回与200 - OK不同的东西200 - OK如果代码抛出exception则返回200 - OK实际上,我认为它比带结果类型的方法更昂贵,因为asp.net框架需要获取异常并将其更改像我之前说过的那样http message到http message 。
如果你在你的action方法中指定一个http message ,你就知道你对客户端的响应(我的意思是,至少是http status code )。 我喜欢使用HttpResponseMessage并使用特定的http status code创建响应。
Every message from an asp.net web api application return an http status code. An exception is expensive, so, if you pass this to the web api, the framework should know how to lead with this and change to a http message result.
There is no extremelly difference. A void action method in web api will return something diferent from 200 - OK if the code throw an exceptionActually I think it's even more expensive than a method with a type in result because the asp.net framework need to get the exception and change it to a http message as I said before.
If you specify an http message in your action method, you know what exactly you are responding to the client (I mean, the http status code as least). I enjoy working with HttpResponseMessage and create responses with the specific http status code.
更多推荐
发布评论