爱上MVC3~在控制器或Action上动态设定模板页(Layout)_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > 爱上MVC3~在控制器或Action上动态设定模板页(Layout)

爱上MVC3~在控制器或Action上动态设定模板页(Layout)

 2013/10/31 12:12:58  张占岭  博客园  我要评论(0)
  • 摘要:回到目录很多境况下,我们需要设置自己模块的layout,即它的布局页面,在MVC2中叫它模板页面,你可以在returnview方法时设置它,当然,这不是一种好方法,因为我不想每个action都去设置一次,因为我们的controller一般指一个模块,而一个模块下的action,它们的layout基本是相同的,所以,有没有一种方法,在controller级别来实现这个呢,呵呵,当然有,那就是attribute特性,我们在MVC环境下,有一个ActionFilterAttribute
  • 标签:模板 MVC

回到目录

很多境况下,我们需要设置自己模块的layout,即它的布局页面,在MVC2中叫它模板页面,你可以在return view方法时设置它,当然,这不是一种好方法,因为我不想每个action都去设置一次,因为我们的controller一般指一个模块,而一个模块下的action,它们的layout基本是相同的,所以,有没有一种方法,在controller级别来实现这个呢,呵呵,当然有,那就是attribute特性,我们在MVC环境下,有一个ActionFilterAttribute,这个想毕大家都听说过,它记录了页面在渲染前与渲染后的状态,这个特性(或者叫它过滤器)的代码如下:

 // 摘要:
    //     Represents the base class for filter attributes.
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
    public abstract class ActionFilterAttribute : FilterAttribute, IActionFilter, IResultFilter
    {
        // 摘要:
        //     Initializes a new instance of the System.Web.Mvc.ActionFilterAttribute class.
        protected ActionFilterAttribute();

        // 摘要:
        //     Called by the ASP.NET MVC framework after the action method executes.
        //
        // 参数:
        //   filterContext:
        //     The filter context.
        public virtual void OnActionExecuted(ActionExecutedContext filterContext);
        //
        // 摘要:
        //     Called by the ASP.NET MVC framework before the action method executes.
        //
        // 参数:
        //   filterContext:
        //     The filter context.
        public virtual void OnActionExecuting(ActionExecutingContext filterContext);
        //
        // 摘要:
        //     Called by the ASP.NET MVC framework after the action result executes.
        //
        // 参数:
        //   filterContext:
        //     The filter context.
        public virtual void OnResultExecuted(ResultExecutedContext filterContext);
        //
        // 摘要:
        //     Called by the ASP.NET MVC framework before the action result executes.
        //
        // 参数:
        //   filterContext:
        //     The filter context.
        public virtual void OnResultExecuting(ResultExecutingContext filterContext);
    }

OnActionExecutedOnActionExecutingOnResultExecutedOnResultExecuting它们记录一个action从加载到页面最终显示在浏览器上的全过程,这个东西我们一般用在页面权限验证,layout页面控制上,它们的执行次序是:
OnActionExecuting  action执行前
OnActionExecuted action执行后
OnResultExecuting 页面渲染前
显示页面的内容
OnResultExecuted 页面渲染结果
如图所示

ARPIx/tvUko9tjr6nEisPqaUumn/x6PPCUAJMrU4s/9O1f2V6GZn9t+p9lzq5d5nWjcp1eO1q48p+zcK09BPEajzIWKrRemqTipK++1ZHVz2wmpRkXrur23DJLKt0253Vvc47VPduf9MkeuTblno5NU9St3UOpMfGUPLNcAiUOc91nkhDLXopaodtRikYJhqYRewIvWMWoDNtrx1dltk2ruKNJSsZReYVl/ao3ro9qH7bvlcfnBFoM6HW+eaWvRW1ZatTBIZlOk0atIp4DjVM0wV29LOY46bup3tzP47lXpsTz46M9vT6h6V/dUc/uhDsz2XsgT1RC7t6f61026bQ6rsLt67W+OvNJH0LllZkltko0g9S+4YSksn8VpkieRVESmCv6LMC9Vjq23qvOc6L5xsn1UdVgsxA0ULpeuBVMBK1HPvbXvUFg2GR6oW9mNe3ZO2RdPhqzffTtJkb9r/cdIoux3Snkrfuf9MzHK5s2+tcQt3l3tUR/PSmO4aN32xZ2pF7qlHuVm1upNSN925f5U6t7JaRi18yQq+o1wtyle1kFq4XEZWk5nBQNVibNr2qC0aDI9iapHOi5MuZAxC0yarOT2tQdDH+/ckx52pbnf45lxyU+zuodn6pXwM5CZo/lfOqreTh4rgy6pxSV7V1Hmvde5LNp1DZNnrptBjVUvTlAIZMG5UQC2kZ1SJeu69bY/aosHwkD1RSa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YxT8G01Np/3RsVUIuynqhkStFxknXzY81F5tc/evHuQJbsbISLmWcPTxQATAayJyqxubknyrSeulp8tOy4gMxhuDZs/7K2nF54blFWLbLJkG3W3fzI6xAl1cJNVr8EtQCAiUFY5c7soM+Pb6uFoS7OoNtn0D0Tlz7VQvcOGesZbn4skfvS4x911VHIUmRuYU3LXL0Z+ZMGAOiHbb5hcioexdTCWOW2lzoyC2sev7L+gn8vrLFm3nUuRa7SREs32cXyUzDx7pm+ZD0pjPxJAwD0wzZjRGwbvuJqYe27NSym7YbqWO9SZKf9wXL75NOdbmoBg25ugsokML2RlB/PXMSXJScb/mQdHUItAGDcqeaXPyaQkT9pAIB+QC1QCwCAOKgFagEAEAe1QC0AAOIQOw8AAOKgFgAAEAe1AACAOKgFAADEQS0AACAOagEAAHFQCwAAiINaAABAHNQCAADioBYAABAHtQAAgDioBQAAxEEtAAAgDmoBAABxUAsAAIiDWgAAQBzUAgAA4qAWAAAQB7UAAIA4qAUAAMRBLQAAIA5qAQAAcVALAACIg1oAAEAc1AIAAOKgFgAAEAe1AACAOKgFAADEQS0AACAOagEAAHFQCwAAiPP/AYrB7nzb7JrNAAAAAElFTkSuQmCC" alt="" />

我们通过ActionFilterAttribute的特性,写个派生类,然后去覆写它的OnActionExecuted方法,将layout页面修改(注意是修改,因为在action执行之后layout已经是默认的布局页了)

代码如下:

 

 /// <summary>
    /// 自定义模板页面
    /// </summary>
    public class LayoutAttribute : ActionFilterAttribute
    {
        private readonly string _masterName;
        public LayoutAttribute(string masterName)
        {
            _masterName = masterName;
        }
        /// <summary>
        /// 页面渲染结束后执行
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            base.OnActionExecuted(filterContext);
            var result = filterContext.Result as ViewResult;
            if (result != null)
            {
                result.MasterName = _masterName;
            }
        }
    }

 

最后把它应用到controller或者action上,使用很简单

回到目录

 

发表评论
用户名: 匿名