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?
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.
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.
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.
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.
The ReasonPhrase does not work in Cassini. Use IIS Express rather than Cassini and you will find that the response includes the reason.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With