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());
}