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 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()); }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。