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

    Filter:
1 /// <summary>
/// 检测是否是手机访问
/// </summary>
public class IsMobileFilter : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
System.Web.HttpBrowserCapabilitiesBase myBrowserCaps = filterContext.HttpContext.Request.Browser;
bool flag = myBrowserCaps.IsMobileDevice;
if (flag)
{
string urls = "www.baidu.com";
if (!string.IsNullOrEmpty(urls))
{
urls = urls.Contains("http") ? urls.Trim() : "http://" + urls.Trim();
filterContext.Result = new RedirectResult(urls);
}
else
{
base.OnActionExecuting(filterContext);
}
}
else
{
base.OnActionExecuting(filterContext);
}
}
}     在FilterConfig中注册全局Filter    
      public static void RegisterGlobalFilters(GlobalFilterCollection filters)
 {
  filters.Add(new IsMobileFilter());
 }

  

上一篇:401 - 未授权:由于凭据无效,访问被拒绝”在iis的解决办法


下一篇:SpringMVC配置