Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating new IHttpActionResult action result methods

Is there a way I can use the new IHttpActionResult interface to return a HttpStatusCode.NoContent response message?

I am currently using return new HttpResponseMessage( HttpStatusCode.NoContent ); and would like to convert this into return NoContent();.

IHttpActionResult has already got Ok(), Conflict() and NotFound() but I cannot find any for Forbidden() and NoContent() which I need to use in my project.

How easy is it to add other result types?

like image 340
Intrepid Avatar asked Feb 05 '15 11:02

Intrepid


People also ask

What is HTTP action result?

Leverage action results to return data as an HttpResponseMessage object from your Web API controller method. ASP.Net Web API is a lightweight framework used for building stateless and RESTful HTTP services. You can take advantage of Action Results in Web API to return data from the Web API controller methods.

What is an advantage of returning an IHttpActionResult from an action method over using a model type?

Here are some advantages of using the IHttpActionResult interface: Simplifies unit testing your controllers. Moves common logic for creating HTTP responses into separate classes. Makes the intent of the controller action clearer, by hiding the low-level details of constructing the response.

Which action results support ASP Net Web API?

There are four Action Results supported by ASP.NET Web API 2. They are: HttpResponseMessage. IHttpActionResult.


2 Answers

There's no convenience method for no-content result because, by default, when a action returns void, the response will have the HTTP status 204.

If you wish to explicitly indicate that on the action, you could also return a StatusCode(HttpStatusCode.NoContent) from your action or a

ResponseMessage(new HttpResponseMessage(HttpStatusCode.NoContent)). 

The Unauthorized() convenience method gives you a 401 status so, for Forbidden (403), you would also have to use StatusCode(HttpStatusCode.Forbidden) or

ResponseMessage(new HttpResponseMessage(HttpStatusCode.Forbidden)) 
like image 160
Rafael Companhoni Avatar answered Oct 05 '22 21:10

Rafael Companhoni


I found this example site that shows how to add a custom IHttpActionResult method and I've used this to create the Forbidden() and NoContent() methods with great success.

public abstract class CommonApiController : ApiController {     public class ForbiddenResult : IHttpActionResult     {         private readonly HttpRequestMessage _request;         private readonly string _reason;          public ForbiddenResult(HttpRequestMessage request,string reason)         {             _request = request;             _reason = reason;         }          public ForbiddenResult(HttpRequestMessage request)         {             _request = request;             _reason = "Forbidden";         }          public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)         {             var response = _request.CreateResponse(HttpStatusCode.Forbidden,_reason);             return Task.FromResult(response);         }     }      public class NoContentResult : IHttpActionResult     {         private readonly HttpRequestMessage _request;         private readonly string _reason;          public NoContentResult(HttpRequestMessage request,string reason)         {             _request = request;             _reason = reason;         }          public NoContentResult(HttpRequestMessage request)         {             _request = request;             _reason = "No Content";         }          public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)         {             var response = _request.CreateResponse(HttpStatusCode.NoContent,_reason);             return Task.FromResult(response);         }     } } 

And then I can use it like this:

public class InvoiceController : CommonApiController {     public async Task<IHttpActionResult> Post([FromBody]Invoice invoice)     {         if(User.IsInRole("Readonly"))         {             return Forbidden();         }          // Rest of code      } } 
like image 21
Intrepid Avatar answered Oct 05 '22 22:10

Intrepid