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.