asp.net mvc uses a custom modelbinder filter keyword method (with demo source download) _ Practical Tips

Source: Internet
Author: User

This example describes how MVC uses custom Modelbinder to filter keywords. Share to everyone for your reference, specific as follows:

The previous article mainly explained how to use actionfilter filter keywords, this article mainly explains how to use their own build Modelbinder to filter the keywords.

First, we use the entity classes in the previous "ASP.net mvc" method of using ActionFilterAttribute to filter keywords, but we need to add the datatype feature, In order to facilitate our construction of the Modelbinder through the Datatypename identified:

Using System.ComponentModel.DataAnnotations;
Using SYSTEM.WEB.MVC;

Namespace Mvcapplication1.models
{public
   class Testmodel
   {public
     int TID {get; set;}
  
     [DataType ("Tname")]
     public string Tname {get; set;}
  
     [DataType ("Tsite")]
     public string Tsite {get; set;}}}


Then we create a new Filtermodelbinder class with the following contents:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using SYSTEM.WEB.MVC;

Namespace MvcApplication1
{public
   class Filtermodelbinder:defaultmodelbinder
   {public
     override Object Bindmodel (ControllerContext controllercontext, Modelbindingcontext BindingContext)
     {
       var Valueshouldfilter = BindingContext.ModelMetadata.DataTypeName;
       if (Valueshouldfilter = = "Tname" | | | valueshouldfilter = = "Tsite")
       {
         var resultprovider = BindingContext.ValueProvider.GetValue (bindingcontext.modelname);
         if (Resultprovider!= null)
         {
           string result = Resultprovider.attemptedvalue;
           result = result. Replace ("<", "<"). Replace (">", ">");
           return result;
         }
  
       Return base. Bindmodel (ControllerContext, BindingContext);
     }
   }

 

The 13th line is mainly to get the datatypename we need to verify.

Line 15th, get the value that needs to be validated, then replace it, and finally return it.

After the above is done, in the Global.asax, we need to specify:

protected void Application_Start ()
{
   arearegistration.registerallareas ();

   Webapiconfig.register (globalconfiguration.configuration);
   Filterconfig.registerglobalfilters (globalfilters.filters);
   Routeconfig.registerroutes (routetable.routes);
   Bundleconfig.registerbundles (bundletable.bundles);

   ModelBinders.Binders.DefaultBinder = new Filtermodelbinder ();
}

In this way, we can use our own modelbinder, and start the test below:

As shown in the above image, when you click the "Add" button, the following error prompts are displayed:

It seems that the system will automatically detect our input values, found that there are illegal characters, will be pop-up error prompts, fortunately, we can configure through the web.config, let it pass the verification:

Open the outermost Web.config and enter the following nodes:

<configuration>
  <system.web>
   
 

Then save, run, we see that the system runs successfully, and the final results are as follows:

We can see that through our custom Modelbinder, the system automatically replaces illegal characters, which is very convenient.

With AOP in MVC, we can now use existing knowledge to make a global filter. Is it convenient to feel?

Full instance code click here to download the site.

I hope this article will help you to ASP.net program design.

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.