Asp.net MVC uses custom ModelBinder to filter keywords (with demo source code download), mvcmodelbinder
This example describes how MVC uses custom ModelBinder to filter keywords. We will share this with you for your reference. The details are as follows:
The previous article mainly explains how to use ActionFilter to filter keywords. This article mainly explains how to use your own ModelBinder to filter keywords.
First, we still use the entity class in the previous article "asp.net MVC uses ActionFilterAttribute to filter keywords", but we need to add the ype feature so that the ModelBinder we constructed can be identified by DataTypeName:
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; } }}
Create a new FilterModelBinder class with the following content:
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); } }}
Row 13th is used to obtain the DataTypeName to be verified.
Row 3: obtain the value to be verified, replace the value, and return the value.
After completing the preceding steps, in Global. asax, We need to specify the following:
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. Let's start the test:
The entered content is shown in. When you click "add", the following error message is displayed:
It seems that the system will automatically detect our input values and find illegal characters. An error message will pop up. Fortunately, we can configure it through web. config to pass Verification:
Open the Web. config on the outermost layer and enter the following nodes:
<configuration> <system.web>
Then save and run. We can see that the system is successfully run and the final result is as follows:
We can see that through our custom ModelBinder, the system automatically replaces invalid characters, which is very convenient.
Everywhere in MVC, we can use the existing knowledge to make a global filter. Is it very convenient?
Click here to download the complete instance code.
I hope this article will help you design your asp.net program.
Articles you may be interested in:
- Solution to the null problem after asp.net mvc UpdateModel updates the object
- Solutions to errors caused by ASP. net mvc namespaces
- ASP. net mvc implements the instrumentation Program
- Development of ASP. NET Mvc with EF delayed Loading
- ASP. NET Mvc development-query data
- ASP. NET Mvc development-delete and modify data
- Details on the use of HtmlHelper controls in ASP. NET MVC
- ASP. net mvc Form Verification
- ASP. NET MVC4 HtmlHelper extension class to implement Paging
- ASP. net mvc uses ActionFilterAttribute to implement permission restrictions (with demo source code download)
- ASP. net mvc @ Helper method and @ functons User-Defined Function usage method