Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC4 WebAPI Reason Phrase?

According to the HttpResponseMessage documentation on MSDN, the reason phrase (as in, the "OK" part of 200 OK) should be settable. The HTTP response does let me set the reason phrase:

HttpResponseMessage response = new HttpResponseMessage(System.Net.HttpStatusCode.Conflict);
            response.ReasonPhrase = "conflict message";

However, when I consume the response on the client side as a WebResponse, I don't see my custom reason phrase. I'd expect to find it under the StatusDescription. Looking at the raw response using Fiddler, it doesn't seem that the reason phrase gets set on the server.

A helpful coworker pointed out that with action results (and derived), I could do something akin to:

new HttpStatusCodeResult(System.Net.HttpStatusCode.Conflict, "conflict message");

It seems as though that is the precise functionality I'm after, but I'm uncertain how to convince WebAPI to cooperate.

Where am I going wrong?

like image 209
Ross Avatar asked Mar 20 '12 20:03

Ross


People also ask

Why We Use Web API instead of MVC?

There are many differences between MVC and Web API, including: We can use the MVC for developing the Web application that replies as both data and views but the Web API is used for generating the HTTP services that replies only as data.

Can we return view from Web API?

You can return one or the other, not both. Frankly, a WebAPI controller returns nothing but data, never a view page. A MVC controller returns view pages. Yes, your MVC code can be a consumer of a WebAPI, but not the other way around.

What are the differences between Web API and Web API 2?

Actually WebAPI 2.0 is enhanced feature of WebApi there is no difference between this two. In version 2.0, the Web API framework has been enhanced to support the following features: IHttpActionResult return type. A new Routing Attribute.

Who can consume Web API?

Almost any native application running on a mobile device other than the Windows one can use ASP.NET Web API as backend. Hence, a web API is good for using with native applications which require web services but not SOAP support.


1 Answers

The ReasonPhrase does not work in Cassini. Use IIS Express rather than Cassini and you will find that the response includes the reason.

like image 134
Rebecca Avatar answered Oct 02 '22 18:10

Rebecca