In a client server communication client sends a request to a server and server responds with some response message.In this communication the request and response have a specific format.Response returned by the server has the following format
Status denotes the success or failure
Header additional information about the response
Message-body actual response
WebAPI return types
HTTP service we develop using WebAPI can return different types of information.WebAPI can return the following types
- Simple or complex types Inbuilt types or types which we define such as class
- No return type or void No value is returned
- HTTPResponseMessage Gives more control over the returned value
- IHTTPAction in WebAPI2 A way of creating HTTPResponseMessage
HTTPResponseMessage in WebAPI
HTTPResponseMessage in WebAPI is one of the types which can be returned from an action method.We can use this type to set the different response values.We can use it to set the response values such as Headers and status code.
Use of HTTPResponseMessage
If we can directly return a specific data type from a WebAPI action method then what is the need of using HTTPResponseMessage.Though we can directly data from a WebAPI action method as:
public Student Get(int id) { return StudentRepository.GetById(id); }
The above method will return a data type of Student type with status of success.The value of Student type is passed in the message body.
So if we directly return a specific data type from a method then we don’t have much control over the other values in the http response such as status code and header values.
If we use HTTPResponse message then we can set the response values such as Header values and status code.
How to use HTTPResponseMessage
We can return HTTPResponseMessage from action method using the CreateResponse extension method.This method is defined in the System.Net.Http.Formatting namespace.We need to add the reference to System.Net.Http.Formatting.dll assembly to use this method.
So we can return the student object using HTTPResponseMessage object using CreateResponse method as:
Request.CreateResponse<Student>(HttpStatusCode.OK, student);
Some of the useful properties defined by the HTTPResponseMessage class are:
Content For retrieving or setting HTTP response message.
Headers For retrieving HTTP response headers.
RequestMessage For retrieving the request message for the current response.
StatusCode For retrieving or setting the status code of the HTTP response.
Version For retrieving or setting the HTTP message version.
So HTTPResponseMessage return type gives more control over the response returned to the client.We can use the properties defined by this type to set the different response values.
We can also create an instance of HttpResponseMessage as:
var response = new HttpResponseMessage(HttpStatusCode.OK);
We can use the HttpResponseException type to return exceptions from action methods.We can use this type to return status codes corresponding to the exceptions.
We can return HttpResponseException as:
Request.CreateErrorResponse(HttpStatusCode.BadRequest, CustomError); { "Message": "The request has error", "ErrorCode": 40 }
Leave a Reply