Created
December 15, 2011 00:57
-
-
Save mdellanoce/1479347 to your computer and use it in GitHub Desktop.
ASP.NET MVC (Version 1) XML Model Binder
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(); | |
} | |
} |
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) | |
{ | |
//... | |
} |
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) | |
{ | |
//... | |
} |
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) | |
{ | |
//... | |
} |
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(); | |
} | |
} | |
} |
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment