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.

Related Articles:

Post Footer automatically generated by Add Post Footer Plugin for wordpress.

About Jimmy Bogard

I'm a technical architect with Headspring in Austin, TX. I focus on DDD, distributed systems, and any other acronym-centric design/architecture/methodology. I created AutoMapper and am a co-author of the ASP.NET MVC in Action books.
This entry was posted in ASP.NET MVC. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • Pingback: The Morning Brew - Chris Alcock » The Morning Brew #880

  • AAA

    I believe you need to use a value provider instead of a model binder because the json value provider uses the same technique as a value provider.

    The drawback of using this is technique is that it will bypass ASP.NET Request Validation. http://bit.ly/lkAIyb

  • http://www.facebook.com/epicureanism Ricky Lin

    thanks for your solution and clear explaination, it helps!

  • Anonymous

    Just wanted to point out that I implemented this code but it did not work until I added inputStream.Position = 0; before Deserializing it in the Model Binder.

  • Tim Scott

    You might consider using WCF Web API for this type of scenario.  It has none of the config-hell that WCF is notorious for.  You can easily add it to a “normal” MVC or straight ASP site.  I used it for my most recent web service API and it pretty well solves all the places where MVC falls short.  Glen and company are still working out a few kinks, but it’s pretty good already.

  • http://www.hopev.com/ Viceroy

    I’ve already bookmark this article and will definitely refer this article to all my close friends and colleagues. Thanks for posting! 

  • Brad

    Jimmy,

    With the code above, any data that comes in to the method will be run through the XmlModelBinder.  What if you have a method like:

    public ActionResult Square(Int id, Payload payload){}How could you get it to only run the payload through the XmlModelBinder, but leave the id alone? I am trying to reuse your code to create a ProtobufModelBinder and it is trashing my ids when it tries to deserialize them.

    • Anonymous

      You can specify the binder on individual model members if you want to force a specific model binding.

  • http://www.outsource-website-design.com/ outsourcing websites

    Please look up and have a look at the sky once belongs to us. If the sky is still vast,clouds are still clear, you shall not cry because my leave doesn’t take away the world that belongs to you.

  • Pingback: Getting my Custom Model bound to my POST controller | PHP Developer Resource

  • http://blakewalter.yolasite.com/ AbelBass

    I agree You can specify the binder on individual model members if you want to force a specific model binding.