MVC 4 Web API Action return: Types vs HttpResponseMessage

ⅰ亾dé卋堺 提交于 2019-12-08 15:00:59

问题


What is the difference returning Types e.g.

    public class MyController : ApiController
    {
        public IEnumerable<MyType> Get()...

vs

Returning HttpResponseMessage:

    public class MyController : ApiController
    {
        public HttpResponseMessage Get()...

??

Does MVC wraps types into HttpResponseMessage content object anyways? The result on the page looks the same besides when formatters are explicitly added.

What is the difference to the client?


回答1:


There is no difference.

If you return CLR type, it will be wrapped with HttpResponseMessage anyway.

The only difference is that if you return HttpResponseMessage you get a chance to work directly with HTTP - so you can set headers, bypass MediaTypeFormatters and so on.

It's all a matter of personal preference and/or the activity that's performed in your action.



来源:https://stackoverflow.com/questions/12633174/mvc-4-web-api-action-return-types-vs-httpresponsemessage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!