This is one of my most highly viewed posts. Unfortunately, this post applies to version 1 of ASP.NET MVC (it might also work with version 2 with some small modifications). If that is what you are looking for, then please read on! Otherwise, for more relevant coverage of this topic I recommend Jimmy Bogard's post
I needed a way to receive raw XML in my action methods, rather than form-encoded values. Model binding makes it a piece of cake:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class XmlModelBinder : IModelBinder | |
{ | |
object IModelBinder.BindModel( | |
ControllerContext controllerContext, | |
ModelBindingContext bindingContext) | |
{ | |
var stream = controllerContext.HttpContext.Request.InputStream; | |
using (var reader = new StreamReader(stream)) | |
{ | |
return reader.ReadToEnd(); | |
} | |
} | |
} |
In my action method all I have to do is apply the model binder attribute:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public ActionResult Edit( | |
[ModelBinder(typeof(XmlModelBinder))] string xml) | |
{ | |
//... | |
} |
Now the edit action receives an XML string. Even better, I can get a strongly typed object if it is serializable:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class XmlModelBinder : IModelBinder | |
{ | |
object IModelBinder.BindModel( | |
ControllerContext controllerContext, | |
ModelBindingContext bindingContext) | |
{ | |
var stream = controllerContext.HttpContext.Request.InputStream; | |
var serializer = new XmlSerializer(bindingContext.ModelType); | |
return serializer.Deserialize(stream); | |
} | |
} |
Now my action method looks like this:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public ActionResult Edit( | |
[ModelBinder(typeof(XmlModelBinder))] MyXmlSerializableClass xml) | |
{ | |
//... | |
} |
The model binder attribute is a little too verbose for my taste, so I wrote a custom model binder attribute:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BindXmlAttribute : CustomModelBinderAttribute | |
{ | |
public override IModelBinder GetBinder() | |
{ | |
return new XmlModelBinder(); | |
} | |
} |
Finally my action method is nice and readable:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public ActionResult Edit( | |
[BindXml] MyXmlSerializableClass xml) | |
{ | |
//... | |
} |
No comments:
Post a Comment