码迷,mamicode.com
首页 > 移动开发 > 详细

MVC4 Filter 验证客户端访问类型(移动端、PC端)

时间:2014-09-25 16:32:29      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   ar   div   sp   cti   

    Filter:
1
/// <summary> 2 /// 检测是否是手机访问 3 /// </summary> 4 public class IsMobileFilter : ActionFilterAttribute 5 { 6 public override void OnActionExecuting(ActionExecutingContext filterContext) 7 { 8 System.Web.HttpBrowserCapabilitiesBase myBrowserCaps = filterContext.HttpContext.Request.Browser; 9 bool flag = myBrowserCaps.IsMobileDevice; 10 if (flag) 11 { 12 string urls = "www.baidu.com"; 13 if (!string.IsNullOrEmpty(urls)) 14 { 15 urls = urls.Contains("http") ? urls.Trim() : "http://" + urls.Trim(); 16 filterContext.Result = new RedirectResult(urls); 17 } 18 else 19 { 20 base.OnActionExecuting(filterContext); 21 } 22 } 23 else 24 { 25 base.OnActionExecuting(filterContext); 26 } 27 } 28 }

    在FilterConfig中注册全局Filter    
      public static void RegisterGlobalFilters(GlobalFilterCollection filters)
         {
              filters.Add(new IsMobileFilter());
         }

  

 

MVC4 Filter 验证客户端访问类型(移动端、PC端)

标签:style   blog   http   color   io   ar   div   sp   cti   

原文地址:http://www.cnblogs.com/doscanner/p/3992599.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!