WebAPI 2:IHttpActionResult的响应速度更快吗?(WebAPI 2: is a void response faster then IHttpActionResult?)

编程入门 行业动态 更新时间:2024-10-25 06:33:36
WebAPI 2:IHttpActionResult的响应速度更快吗?(WebAPI 2: is a void response faster then IHttpActionResult?)

我今天在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.

更多推荐

本文发布于:2023-07-16 09:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1126831.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:更快   速度   IHttpActionResult   WebAPI   response

发布评论

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

>www.elefans.com

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