![Class Class](/uploads/1/2/5/4/125499113/895904574.png)
Var response = new HttpResponseMessage(HttpStatusCode.Unauthorized);var tsc = new TaskCompletionSource;tsc.SetResult(response);return tsc.Task;This code snippet will return one Unauthorized HTTP response (not an unauthorized response, but HTTP response of unauthorized type) asynchronously. Now, if we want to change the response type then obviously we must change the status code.
Fine and simple but it is simpler in the Web API 2. We can create the same kind of response with a single line of code.Here you will see how the ASP.NET Web API converts the return value from a controller into an HTTP response message.Please note that the feature is available in Web API 2.0, so please ensure that your application is updated to 2.0 versions before trying the following code. We know that a Web API controller action can return any one of the following. Void. HttpResponseMessage.
Create a new ResponseEntity with the given body and status code, and no headers. ResponseEntity ( T body, MultiValueMap headers, HttpStatus status) Create a new HttpEntity with the given body, headers, and status code.
IHttpActionResult (new in Web API 2.0). Some other data typeNow in today's article we will see the third point with an example. To use IHttpResult in your application, you must include “System.WebHttp” and provide a reference of the 'system.Web.Http' assembly.The interface IHttpActionResult contains one any only one method called “ExecuteAsync”. Here is the definition of the interface.
![Converter Converter](/uploads/1/2/5/4/125499113/939569941.jpg)
CategoryDescription1xx: InformationalCommunicates transfer protocol-level information.2xx: SuccessIndicates that the client’s request was accepted successfully.3xx: RedirectionIndicates that the client must take some additional action in order to complete their request.4xx: Client ErrorThis category of error status codes points the finger at clients.5xx: Server ErrorThe server takes responsibility for these error status codes.Now look at subset of codes that specially apply to the design of a REST APIs – in some more detail. Your 401/403 description is inadequate. Please refer to for a great description.“401 indicates that the resource can not be provided, but the server is REQUESTING that the client log in through HTTP Authentication and has sent reply headers to initiate the process. Possibly there are authorizations that will permit access to the resource, possibly there are not, but let’s give it a try and see what happens.403 indicates that the resource can not be provided and there is, for the current user, no way to solve this through RFC2617 and no point in trying.
This may be because it is known that no level of authentication is sufficient (for instance because of an IP blacklist), but it may be because the user is already authenticated and does not have authority. The RFC2617 model is one-user, one-credentials so the case where the user may have a second set of credentials that could be authorized may be ignored. It neither suggests nor implies that some sort of login page or other non-RFC2617 authentication protocol may or may not help – that is outside the RFC2616 standards and definition.”.