Is it possible to pass an XDocument as a parameter to an action in ASP.NET MVC?

I am wondering if it is possible to write a controller action in ASP.NET MVC that takes an XDocument as a parameter. This, of course, simply means that form mail sends an XML string.

Is there anything special I need to do to accept this as a parameter?

+5
source share
1 answer

You can write a custom type binding and register it in the application launch event handler in global.asax:

protected void Application_Start()
{
    ModelBinders.Binders.Add(typeof(XDocument), new YourXDocumentBinder());
}

The MVC pipeline is automatically called by the binder when it encounters an action with an XDocument argument.

The binder implementation will look something like this:

public class YourXDocumentBinder : DefaultModelBinder
{
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
         // handle the posted data
    }
}
+6
source

All Articles