• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    asp.net MVC利用自定义ModelBinder过滤关键字的方法(附demo源码下载)

    本文实例讲述了MVC利用自定义ModelBinder过滤关键字的方法。分享给大家供大家参考,具体如下:

    前面一篇主要讲解了如何利用ActionFilter过滤关键字,这篇主要讲解如何利用自己打造的ModelBinder来过滤关键字。

    首先,我们还是利用上一篇《asp.net MVC利用ActionFilterAttribute过滤关键字的方法》中的实体类,但是我们需要加上DataType特性,以便于我们构造的ModelBinder通过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; }
       }
    }
    
    

    然后我们新建一个FilterModelBinder的类,其中内容如下:

    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("", "lt;").Replace(">", "gt;");
               return result;
             }
           }
      
           return base.BindModel(controllerContext, bindingContext);
         }
       }
    }
     
    
    

    第13行,主要是获取我们需要验证的DataTypeName.

    第15行,获取需要验证的值,然后替换,最后返回即可.

     上面做完后,在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了,下面开始测试:

    我们输入的内容如上图所示,当点击”添加”按钮的时候,确弹出如下的错误提示:

    看来,系统会自动检测我们的输入值,发现有非法字符,会弹出错误提示,还好我们可以通过web.config配置一下,让其通过验证:

    打开最外层的Web.config,输入以下节点:

    configuration>
      system.web>
      httpRuntime requestValidationMode="2.0" />
      /system.web>
      pages validateRequest="false">
      /pages>
    /configuration>
    
    

    然后保存,运行,我们看到,系统成功跑了起来,最后的结果如下:

    我们可以看到,通过我们自定义的ModelBinder,系统自动将非法字符进行了替换,非常方便。

    MVC中处处AOP,现在我们就可以利用现有的知识做一个全局过滤器了。是不是感觉很方便呢?

    完整实例代码点击此处本站下载。

    希望本文所述对大家asp.net程序设计有所帮助。

    您可能感兴趣的文章:
    • Android深入浅出之Binder机制
    • Android进程间通信(IPC)机制Binder简要介绍
    • 理解Android系统Binder机制
    • Android4.1中BinderService用法实例分析
    • android IPC之binder通信机制
    • 一个用xslt样式将xml解析为xhtml的类TransformBinder(兼容FF和IE7.0)
    • 浅谈Service Manager成为Android进程间通信(IPC)机制Binder守护进程之路
    上一篇:asp.net MVC利用ActionFilterAttribute过滤关键字的方法
    下一篇:剖析ASP.NET MVC的DependencyResolver组件
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯

    时间:9:00-21:00 (节假日不休)

    地址:江苏信息产业基地11号楼四层

    《增值电信业务经营许可证》 苏B2-20120278

    asp.net MVC利用自定义ModelBinder过滤关键字的方法(附demo源码下载) asp.net,MVC,利用,自定义,ModelBinder,