Reputation: 49
I am reading about ASP.NET Web API and the REST architecture. I am aware that using ASP.NET Web API we can create RESTful or non-RESTful services.
According to REST constraints of "Uniform Interface", we need to implement HTTP verbs like GET
, POST
, PUT
, DELETE
, etc. I am aware that if we are implementing HTTP verbs then we need to use the HTTP protocol for communication with the service.
I learned about implementing GET
from the How to Implement GET Method in Web API tutorial and understood how we use HttpResponseMessage
to return an HTTP response.
But then I happened to come across some articles where we haven't implemented any HttpResponseMessage
but instead simple GET
, POST
, PUT
, and DELETE
. See, Creating Web API Application and How to Create Web API in ASP.Net MVC.
My questions:
What is the use of implementing HTTP verbs without HttpResponseMessage
? The whole purpose of implementing GET
, POST
, PUT
, and DELETE
in ASP.NET Web Api is that we can create HTTP responses (as in the second and third tutorials I linked).
If we don't use HttpResponseMessage
, but still implement GET
, POST
, PUT
, and DELETE
, then can we say it's a RESTFul service?
If we don't implement HttpResponseMessage
with HTTP verbs, then how will the response be sent back in a RESTFul service?
Upvotes: 0
Views: 363
Reputation: 32702
What is the use of implementing HTTP verbs without HttpResponseMessage?
A Web API action method can return an HttpResponseMessage if the developer needs tight control over how the HTTP Response is generated. If not, they can just return an object that contains the data.
The whole purpose of implementing GET, POST, PUT, and DELETE in ASP.NET Web Api is that we can create HTTP responses (as in the second and third tutorials I linked).
No, that's not the purpose. Those HTTP verbs are used as part of an HTTP request to express intent about how to process the resource at the URI.
If we don't implement HttpResponseMessage with HTTP verbs, then how will the response be sent back in a RESTFul service?
In a Web API method, there are various ways to return a result. If the method is void (and thus there's no return statement) then a successful result will have an HTTP Status Code of 204 (No Content).
Or you can return a crafted HttpResponseMessage or IHttpActionResult if you want tight control over how the response is generated.
Or you can return any other type of serializble object. The result will be serialized and the HTTP response will have an HTTP Status Code of 200 (Success).
The various return types are well described in the documentation.
Is it mandatory to use the HTTP protocol and the HttpResponseMessage class when building an ASP.NET Web Api?
ASP.NET Web API is built as a web server application and thus around the HTTP protocol. But as described above, you don't have to create an instance of an HttpResponseMessage.
Upvotes: 1