Model binding XML in ASP.NET MVC 3


ASP.NET MVC 3 introduced the concepts of service location to conditionally build providers and factories for various extension points, such as Value Providers, Model Metadata Providers, and notably, Model Binders. Model binders in ASP.NET MVC are responsible for binding contextual HTTP request data (and any other context information) into the action method parameters.

Most of the time, these values are served up through the DefaultModelBinder class, which in turn leans on a collection of Value Providers to, well, provide values to the Model Binder. Value providers are great for centrally modeling dictionary-centric information, such as HTTP request variables (form POST, query string, cookie values etc.)

Model binders operate at one level of abstraction up from value providers, where we take control of the entire object deserialization/resolution/composition step ourselves. XML is one area where we can easily provide deserialization seamlessly from our controller action knowing about it. Let’s look at a simple example of a controller action accepting XML and responding with XML:

public class MathController : Controller
{
    public ActionResult Square(Payload payload)
    {
        var result = new Result
        {
            Value = payload.Value * payload.Value
        };

        return new XmlResult(result);
    }
}

Our input and output models are items easily serializable/deserializable:

public class Payload
{
    public int Value { get; set; }
}

public class Result
{
    public int Value { get; set; }
}

The XmlResult is from MvcContrib, and encapsulates the serialization for us. However, we don’t have anything to accept XML as an input. We could just accept a string value and do the manual deserialization ourselves, but what’s the fun in that?

We’d also like to have the binding done according to the content type of the request payload, so that “text/xml” is recognized and automatically deserialized, just as “application/json” is currently done out of the box. Using RestSharp, we want to get this test to pass:

[Test]
public void RestSharp_Tester()
{
    var client = new RestClient("http://127.0.0.1.:33443");

    var req = new RestRequest("Math/Square", Method.POST);
    
    var body = new Payload
    {
        Value = 5
    };

    req.AddBody(body);

    var resp = client.Execute<Result>(req);

    var value = resp.Data.Value;

    Assert.AreEqual(25, value);
}

Just to make sure we’re not pulling any punches, here’s the actual HTTP request from Fiddler:

POST http://127.0.0.1.:33443/Math/Square HTTP/1.1
Accept: application/json, application/xml, text/json, text/x-json, text/javascript, text/xml
User-Agent: RestSharp 101.3.0.0
Content-Type: text/xml
Host: 127.0.0.1.:33443
Content-Length: 41
Accept-Encoding: gzip, deflate
Connection: Keep-Alive

<Payload>
  <Value>5</Value>
</Payload>

Not that exciting, I know, but you can see from the request that the content type indicated is “text/xml”. Our model binder should detect this and provide a deserialized object from that XML.

To do this, we’ll first need to build a model binder provider. Model binder providers decide on whether or not for the given type that they can provide a model binder. Instead of looking at the type metadata, let’s look at the content type of the incoming request:

public class XmlModelBinderProvider : IModelBinderProvider
{
    public IModelBinder GetBinder(Type modelType)
    {
        var contentType = HttpContext.Current.Request.ContentType;

        if (string.Compare(contentType, @"text/xml", 
            StringComparison.OrdinalIgnoreCase) != 0)
        {
            return null;
        }

        return new XmlModelBinder();
    }
}

We check the incoming request’s content type, and if it matches our “text/xml” type, we return our XmlModelBinder. The XmlModelBinder is rather simple now, shown below.

public class XmlModelBinder : IModelBinder
{
    public object BindModel(
        ControllerContext controllerContext,
        ModelBindingContext bindingContext)
    {
        var modelType = bindingContext.ModelType;
        var serializer = new XmlSerializer(modelType);

        var inputStream = controllerContext.HttpContext.Request.InputStream;

        return serializer.Deserialize(inputStream);
    }
}

We simply build up the built-in XML serializer based on the model type we’re binding to, feeding in the raw Stream from the request. Finally, we need to make sure we add our model binder provider to the global providers collection at application startup (Application_Start):

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();

    ModelBinderProviders.BinderProviders
        .Add(new XmlModelBinderProvider());

    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);
}

This model binder provider could have been provided through the service location option instead of registered manually. With this model binder provider added, our model is correctly bound, and the response returned matches our expectation:

HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Fri, 24 Jun 2011 01:15:55 GMT
X-AspNet-Version: 4.0.30319
X-AspNetMvc-Version: 3.0
Cache-Control: private
Content-Type: text/xml; charset=utf-8
Content-Length: 178
Connection: Close

<?xml version="1.0" encoding="utf-8"?>
<Result>
  <Value>25</Value>
</Result>

The value coming out is just what was returned from the XmlResult, but what’s neat is that our input model is just a POCO that looks like it could have come from a POST from form encoded values, JSON or XML. In fact, they all work!

With just a few lines of code, we were able to effectively add XML support to our HTTP endpoints. It’s certainly not a full REST framework, but it can serve in a pinch in cases we just need to expose simple endpoints for consumers that want to do RPC but don’t want to go the full SOAP route.

We also leave open the option of supporting alternative content types, all seamless to our controller action (except for content negotiation). All without mucking around with the complications of WCF, using the same deployment, development and configuration model of our normal ASP.NET MVC sites.

Cleaning up POSTs in ASP.NET MVC