zoukankan      html  css  js  c++  java
  • 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     }        
    设置页面访问权限验证
  • 相关阅读:
    [转]以安装桌面体验功能为例来探索windows2012服务器管理器的新变化
    [转]DPM2012系列之十六:在SCOM2012上集成DPM2012中央控制台
    [转]DPM2012系列之十二:还原exchange2010用户邮件
    Windows Phone Dev Center How to deploy and run a Windows Phone app
    [转].NET StockTrader 6 Sample Application
    [转]DPM2012系列之一:安装Data Protection Manager 2012
    [转]DPM2012系列之二十:保护Windows server 2012
    [转]DPM2012系列之十七:如何将备份文件恢复到网络共享文件夹
    [转]DPM2012系列之十四:备份SQL server 2008R2数据库
    [转]使用SCOM 2012监控网络
  • 原文地址:https://www.cnblogs.com/ziranquliu/p/4689582.html
Copyright © 2011-2022 走看看