Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC 4 Web API Action return: Types vs HttpResponseMessage

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?

like image 610
T.S. Avatar asked Sep 28 '12 03:09

T.S.


1 Answers

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.

like image 200
Filip W Avatar answered Oct 19 '22 08:10

Filip W