首页 > 代码库 > MVC4 Filter 验证客户端访问类型(移动端、PC端)

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

    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 else19 {20 base.OnActionExecuting(filterContext);21 }22 }23 else24 {25 base.OnActionExecuting(filterContext);26 }27 }28 }

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

  

 

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