Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different return types for ASP.NET Web API

I am attempting to write truly RESTful web services over HTTP using ASP.NET MVC 4 Web API.

The current challenge I have is to return different return types (entity-body) based upon my status code.

For example, for resource Hammer I have a .NET model class "Hammer", and a HammerController:

namespace Awesomeness
{
    public class HammerController : ApiController
    {
        public Hammer Get(int id)
        {
        }
...

If the ID doesn't exist (404) or requires different authorization (401), I can easily shortcut the return and manually set the status code and any other content, which is cool. However, in many non-2xx statuses, I want to return a different entity-body than the Hammer resource representation. I can easily do this manually, but I'd like to take advantage of the ASP.NET MVC 4 Web API automagic serialization and deserialization to/from XML or JSON depending on the request headers.

So my core question is: Can I take advantage of ASP.NET MVC 4 Web API's automagic serialization while returning differing return types?

Some potential approaches I have thought about are:

  1. Have the controller method return the main resource type, but short-circuit the return with HttpContext.Current.Response and somehow hook into the automagic serialization (preferred).

  2. Have my Model class be more like a C union where it represents this type or that type and let it get serialized as part of the normal return process (and just override status code and any response headers needed). Even if I think through how to do this, I have a feeling it would still end up being very hacky.

Edited Apr 27 2012: I can throw an HttpResponseException like this:

HttpResponseMessage response = new HttpResponseMessage(statusCode);
if (!string.IsNullOrWhiteSpace(text))
{
    response.Content = new StringContent(text);
}
throw new HttpResponseException(response);

...but now I need to figure out how to hook into the auto-magic serialization and set the response.Content to the Accept header negotiated representation.

like image 574
MikeJansen Avatar asked Apr 26 '12 15:04

MikeJansen


1 Answers

I derived the clean answer to this question from an answer I received on this other question.

If I make my return value HttpResponseMessage, I can return whatever status or content I need to by returning either a new HttpResponseMessage().

EDIT 5/10/2012: Thanks to input from @DarrelMiller, removed deprecated HttpResponseMessage<T> and replaced with call to HttpRequestMessage.CreateResponse<T>() extension method. NOTE: This extension method is not available in the MVC4/WebAPI beta. You need to build from source or get the nightly build.

Oversimplified example:

public HttpResponseMessage Post(MyResource myResource)
{
    ...
    if (goodStuff)
    {
        return ControllerContext.Request.CreateResponse(HttpStatusCode.Created, myNewResource);
    }
    else if (badStuff)
    {
        return ControllerContext.Request.CreateResponse(HttpStatusCode.BadRequest, badRequest);
    }
    else
    {
        return new HttpResponseMessage(HttpStatusCode.InternalServerError);
    }
}
like image 196
MikeJansen Avatar answered Oct 01 '22 00:10

MikeJansen