码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2015-07-30 16:38:01      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:

技术分享
 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     }        
设置页面访问权限验证

 

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

标签:

原文地址:http://www.cnblogs.com/ziranquliu/p/4689582.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!