Asp .Net MVC中常用过滤属性类

 1         /// <summary>
 2     /// 
 3     /// </summary>
 4     public class AjaxOnlyAttribute : ActionFilterAttribute
 5     {
 6         /// <summary>
 7         /// Called by the ASP.NET MVC framework before the action method executes.
 8         /// </summary>
 9         /// <param name="filterContext">The filter context.</param>
10         public override void OnActionExecuting(ActionExecutingContext filterContext)
11         {
12             if (!filterContext.HttpContext.Request.IsAjaxRequest())
13             {
14                 filterContext.Result = new ContentResult() { Content = "只支持Ajax方法调用。", ContentEncoding = Encoding.UTF8, ContentType = "text/html" };
15                 //new HttpNotFoundResult();
16             }
17             base.OnActionExecuting(filterContext);
18         }
只允许通过Ajax调用
 1         /// <summary>
 2     /// 自定义模板页面
 3     /// </summary>
 4     public class LayoutAttribute : ActionFilterAttribute
 5     {
 6         private readonly string _masterName;
 7         public LayoutAttribute(string masterName)
 8         {
 9             _masterName = masterName;
10         }
11         /// <summary>
12         /// 页面渲染结束后执行
13         /// </summary>
14         /// <param name="filterContext"></param>
15         public override void OnActionExecuted(ActionExecutedContext filterContext)
16         {
17             base.OnActionExecuted(filterContext);
18             var result = filterContext.Result as ViewResult;
19             if (result != null)
20             {
21                 result.MasterName = _masterName;
22             }
23         }
24     }
设置公共母板页面
 1         /// <summary>
 2     /// 页面标题
 3     /// </summary>
 4     public class PageTitleAttribute : ActionFilterAttribute
 5     {
 6         private readonly string _pageTitle;
 7         public PageTitleAttribute(string pageTitle)
 8         {
 9             _pageTitle = pageTitle;
10         }
11         /// <summary>
12         /// 页面渲染结束后执行
13         /// </summary>
14         /// <param name="filterContext"></param>
15         public override void OnActionExecuted(ActionExecutedContext filterContext)
16         {
17             base.OnActionExecuted(filterContext);
18             var result = filterContext.Result as ViewResult;
19             if (result != null)
20             {
21                 result.ViewBag.Title = _pageTitle;
22             }
23         }
24     }
设置Action页面标题
 1     [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
 2     public class AuthorizeFilterAttribute : ActionFilterAttribute
 3     {
 4         // OnActionExecuted 在执行操作方法后由 ASP.NET MVC 框架调用。
 5         // OnActionExecuting 在执行操作方法之前由 ASP.NET MVC 框架调用。
 6         // OnResultExecuted 在执行操作结果后由 ASP.NET MVC 框架调用。
 7         // OnResultExecuting 在执行操作结果之前由 ASP.NET MVC 框架调用。
 8         public override void OnActionExecuting(ActionExecutingContext filterContext)
 9         {
10             //DoSomething();
11         }
12     }        
设置页面访问权限验证
原文地址:https://www.cnblogs.com/ziranquliu/p/4689582.html