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?
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.
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.
There are four Action Results supported by ASP.NET Web API 2. They are: HttpResponseMessage. IHttpActionResult.
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))
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 } }
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