1、新建MasterFilterAttribute类继承ActionFilterAttribute,重写方法OnActionExecuted

,指定ViewResult的MasterName = "Master";

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace MvcApplication1
{
public class MasterFilterAttribute : ActionFilterAttribute
{ public override void OnActionExecuted(ActionExecutedContext filterContext)
{ base.OnActionExecuted(filterContext); var result = filterContext.Result as ViewResult; if (result != null)
{
result.MasterName = "Master";
}
} }
}

2、在Global.asax中注册

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing; namespace MvcApplication1
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801 public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{ filters.Add(new MasterFilterAttribute()); }
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
} protected void Application_Start()
{
AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
}
}

3、创建Master页面,Views\Shared\Master.cshtml

<!DOCTYPE html>

<html>

<head>

<title>@ViewBag.Title</title>

<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />

<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>

</head>

<body>

<h2>master-top</h2>

<div id="main">

@RenderBody()

</div>

<h2>master-bottom</h2>

</body>

</html>

4、创建子页面Home,Views\Home\Home.cshtml

@{

ViewBag.Title = "Index";

}

<h2>Index</h2>

5、效果图

一种在MVC3框架里面设置模板页的方法,不使用_ViewStart的更多相关文章

  1. ecshop后台增加模板页的方法

    CShop的动态模板机制是一个非常灵活的系统,管理员可以在后台根据自己的要求调整模板模块的显示位置.本文详细讲解了如何修改ECSHOP内部结构使得用户可以添加自己的模板页从而方便灵活的使用系统自带的模 ...

  2. ASP.Net MVC 布局页 模板页 使用方法详细说明

    一.Views文件夹 -> Shared文件夹下的 _Layout.cshtml 母版页 @RenderBody 当创建基于_Layout.cshtml布局页面的视图时,视图的内容会和布局页面合 ...

  3. 三种查看SqlServer中数据物理pge页的方法

    1.根据数据记录查看当前记录所在的文件编号.page页.以及在页中的插槽. 示例如下: SELECT top %%physloc%%, sys.fn_physlocFormatter (%%physl ...

  4. ASP.NET MVC3细嚼慢咽---(2)模板页

    这一节演示下MVC3中怎样使用模板页,在传统的webform设计模式中,我们使用masterpage作为模板页,在MVC3 razor视图设计中,我们使用另一种方式作为模板页. 新建一个MVC3项目, ...

  5. ASP.NET MVC学习之路:模板页

    1.MVC开发步骤: 控制器-视图. 2.每一个视图都会有一个默认的模板页:_ViewStart.cshtml. 名字不能改,只能叫_ViewStart. 3.Layout=”~/Views/Shar ...

  6. .Net Mvc3框架调用服务端控件解决方案

      /*BY:Wangyexin date:2011年4月30日 20:17:38*/ /*说明:.net mvc3框架,View层调用服务端控件,输出到.cshtml文件中显示*/ 1.先说说.ne ...

  7. Spring框架的JDBC模板技术和事物

    Spring框架的JDBC模板技术         技术分析之Spring框架的JDBC模板技术概述  1. Spring框架中提供了很多持久层的模板类来简化编程,使用模板类编写程序会变的简单     ...

  8. DotNet 资源大全中文版,内容包括:编译器、压缩、应用框架、应用模板、加密、数据库、反编译、IDE、日志、风格指南等

    DotNet 资源大全中文版 我想很多程序员应该记得 GitHub 上有一个 Awesome - XXX 系列的资源整理.awesome-dotnet 是由 quozd 发起和维护.内容包括:编译器. ...

  9. 一种轻便且灵活的js模板的思路

    一种轻便且灵活的js模板的思路 项目地址:https://github.com/j20041426/template 思路背景 在Vue.React.Angular等大前端框架异军突起的今天,写前端时 ...

随机推荐

  1. [/wp_active.php]

    if ( !is_multisite() ) { wp_redirect( site_url( '/wp-login.php?action=register' ) );//将用户重定向到一个预先制定的 ...

  2. POJ1330 Nearest Common Ancestors(最近公共祖先)(tarjin)

    A - Nearest Common Ancestors Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld &am ...

  3. 转:mysql group by 用法解析(详细)

    group by 用法解析 group by语法可以根据给定数据列的每个成员对查询结果进行分组统计,最终得到一个分组汇总表. SELECT子句中的列名必须为分组列或列函数.列函数对于GROUP BY子 ...

  4. Annotation的语法和使用

    http://blog.csdn.net/cdl2008sky/article/details/6265742 (1) .<context:component-scan base-package ...

  5. [Contest20180316]Mythological IV

    令$S(n)=\sum\limits_{i=0}^{n-1}f(i)q^i$,那么存在一个次数$\leq k$的多项式使得$S(n)=q^ng(n)-g(0)$(证明来自杜教的PPT) 设$f$的次数 ...

  6. [BZOJ1513]Tet-Tetris 3D

    get了新的标记永久化技能- 这题要求询问max和覆盖,因为是线段树套线段树,所以内外都不可以标记下传 这种标记永久化的套路是维护两个标记:$mx,all$,$mx$表示这个子树内的真最大值,$all ...

  7. python基础-匿名函数、内置函数、正则表达式、模块

    1. 匿名函数 1.1 有名函数 有名函数:定义了一个函数名,函数名指向内存地址:通过函数名进行访问.函数名加括号就可以运行有名函数,例如:func() def func(x, y, z = 1): ...

  8. 找出能被5或6整除,但是不能被两者同时整除的数 Exercise05_11

    /** * @author 冰樱梦 * 时间:2018年下半年 * 题目:找出能被5或6整除,但是不能被两者同时整除的数 * */ public class Exercise05_11 { publi ...

  9. mybatis-xml特殊字符处理

    1. 使用CDATA区: 它的全称为character data,以"<![CDATA[ "开始,以" ]]>" 结束,在两者之间嵌入不想被解析程序 ...

  10. 安装scrapy报错问题解决

    今天在安装scrapy时候,最后一步出现下面报错(操作系统为centerOS 6.4) error: Setup script exited with error: command 'gcc' fai ...