MVC Learning Series -- ModelBinder extension and mvcmodelbinder Extension

Source: Internet
Author: User

MVC Learning Series -- ModelBinder extension and mvcmodelbinder Extension

In the MVC system, we accept data and use the ModelBinder technology.

MVC Learning Series-ActionResult extension in this series, we customize the returned results of XmlResult.

Does it mean that we can POST an XML data type to our project. In this case, we need to customize an XmlModelBinder to accept XML data.

Create XmlModelBinder, inherited from: IModelBinder

 1 public class XmlModelBinder : IModelBinder 2     { 3         public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) 4         { 5             try 6             { 7                 var modelType = bindingContext.ModelType; 8                 var serializer = new XmlSerializer(modelType); 9                 var inputStream = controllerContext.HttpContext.Request.InputStream;10                 return serializer.Deserialize(inputStream);11             }12             catch (Exception)13             {14                 throw;15             }16         }17     }

In PostXmlStudent of HomeController, use:

1 public XmlResult PostXmlStudent ([ModelBinder (typeof (XmlModelBinder)] StudentViewModel viewModel) 2 {3 viewModel. name = "Result"; 4 // return the Result in XML format again. 5 return new XmlResult (viewModel); 6}

Use Fiddler to simulate a POST request with XML data

Accept data:

Because viewModel. Name = "Result ";
Therefore, view the returned data in Fiddler:

 

 

In this way, you can simply implement the function of receiving XML data structures.

However, this creates a problem, that is, when you need XML, you must add the [ModelBinder (typeof (XmlModelBinder)] code, which is very troublesome.

The question is: can MVC be automatically identified. The answer to this question is yes.

Create XmlModelBinderProvider and inherit from IModelBinderProvider

1 public class XmlModelBinderProvider: IModelBinderProvider 2 {3 public IModelBinder GetBinder (Type modelType) 4 {5 var contentType = HttpContext. current. request. contentType. toLower (); 6 7 // if it is not Text/Xml, null 8 if (contentType! = "Text/xml") 9 {10 return null; 11} 12 13 return new XmlModelBinder (); 14} 15}

Note that injection is required in the Global class.

1 protected void Application_Start () 2 {3 AreaRegistration. registerAllAreas (); 4 FilterConfig. registerGlobalFilters (GlobalFilters. filters); 5 RouteConfig. registerRoutes (RouteTable. routes); 6 BundleConfig. registerBundles (BundleTable. bundles); 7 8 // load the configuration file 9 var logCfg = new FileInfo (AppDomain. currentDomain. baseDirectory + "Log4Net. config "); 10 XmlConfigurator. configureAndWatch (logCfg); 11 12 // XmlModelBinderProvider injects ModelBinderProviders13ModelBinderProviders. BinderProviders. Insert (0, new XmlModelBinderProvider ());14}

Create an Action, PostXmlStudent_Auto. In particular, ModelBinder is not used.

1 public XmlResult PostXmlStudent_Auto (StudentViewModel viewModel) 2 {3 viewModel. Name = "PostXmlStudent_Auto"; 4 // return the result 5 return new XmlResult (viewModel) in XML format again; 6}

Use fiddle to simulate a request:

Accept data:

Because viewModel. Name = "PostXmlStudent_Auto ";
, So fiddle returns data:

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.