asp.net MVC利用自定義ModelBinder過濾關(guān)鍵字的方法(附demo源碼下載)
本文實(shí)例講述了MVC利用自定義ModelBinder過濾關(guān)鍵字的方法。分享給大家供大家參考,具體如下:
前面一篇主要講解了如何利用ActionFilter過濾關(guān)鍵字,這篇主要講解如何利用自己打造的ModelBinder來過濾關(guān)鍵字。
首先,我們還是利用上一篇《asp.net MVC利用ActionFilterAttribute過濾關(guān)鍵字的方法》中的實(shí)體類,但是我們需要加上DataType特性,以便于我們構(gòu)造的ModelBinder通過DataTypeName識(shí)別出來:
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; }
}
}
然后我們新建一個(gè)FilterModelBinder的類,其中內(nèi)容如下:
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);
}
}
}
第13行,主要是獲取我們需要驗(yàn)證的DataTypeName.
第15行,獲取需要驗(yàn)證的值,然后替換,最后返回即可.
上面做完后,在Global.asax中,我們需要指定一下:
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();
}
這樣,我們就能使用我們自己的ModelBinder了,下面開始測(cè)試:

我們輸入的內(nèi)容如上圖所示,當(dāng)點(diǎn)擊”添加”按鈕的時(shí)候,確彈出如下的錯(cuò)誤提示:

看來,系統(tǒng)會(huì)自動(dòng)檢測(cè)我們的輸入值,發(fā)現(xiàn)有非法字符,會(huì)彈出錯(cuò)誤提示,還好我們可以通過web.config配置一下,讓其通過驗(yàn)證:
打開最外層的Web.config,輸入以下節(jié)點(diǎn):
<configuration> <system.web> <httpRuntime requestValidationMode="2.0" /> </system.web> <pages validateRequest="false"> </pages> </configuration>
然后保存,運(yùn)行,我們看到,系統(tǒng)成功跑了起來,最后的結(jié)果如下:

我們可以看到,通過我們自定義的ModelBinder,系統(tǒng)自動(dòng)將非法字符進(jìn)行了替換,非常方便。
MVC中處處AOP,現(xiàn)在我們就可以利用現(xiàn)有的知識(shí)做一個(gè)全局過濾器了。是不是感覺很方便呢?
完整實(shí)例代碼點(diǎn)擊此處本站下載。
希望本文所述對(duì)大家asp.net程序設(shè)計(jì)有所幫助。
相關(guān)文章
asp.net 需要登陸的網(wǎng)站上下載網(wǎng)頁源代碼和文件
最近有個(gè)項(xiàng)目需要從網(wǎng)絡(luò)上下載網(wǎng)頁信息和文件,并且需要登錄后才能下載,所以做了個(gè)下載的通用類,供大家參考。2009-05-05
GridView自定義分頁實(shí)例詳解(附demo源碼下載)
這篇文章主要介紹了GridView自定義分頁的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了GridView自定義分頁所涉及的樣式布局及功能實(shí)現(xiàn)相關(guān)技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2016-03-03
如何將數(shù)據(jù)綁到gridview然后導(dǎo)成excel
這篇文章主要介紹了如何將數(shù)據(jù)綁到gridview然后導(dǎo)成excel,需要的朋友可以參考下2014-02-02
ASP.NET從字符串中查找字符出現(xiàn)次數(shù)的具體實(shí)現(xiàn)方法
今天在一場(chǎng)“特殊的討論”中引入了一個(gè)問題,如何在C#求出字符串中某字符的出現(xiàn)次數(shù),比如求“ADSFGEHERGASDF”中“A”出現(xiàn)的次數(shù)2013-11-11
asp.net頁面與頁面之間傳參數(shù)值方法(post傳值和get傳值)
這篇文章主要介紹了asp.net頁面與頁面之間傳參數(shù)值方法,說明了post傳值和get傳值的使用方法,需要的朋友可以參考下2014-02-02
asp.net代碼中修改web.config節(jié)點(diǎn)的具體方法
在有些情況下,要在代碼中讀取一種全局變量,把這種全局變量放在web.config是一種常見的手段。2013-06-06

