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

,指定ViewResult的MasterName = "Master";

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6.  
  7. namespace MvcApplication1
  8. {
  9. public class MasterFilterAttribute : ActionFilterAttribute
  10. {
  11.  
  12. public override void OnActionExecuted(ActionExecutedContext filterContext)
  13. {
  14.  
  15. base.OnActionExecuted(filterContext);
  16.  
  17. var result = filterContext.Result as ViewResult;
  18.  
  19. if (result != null)
  20. {
  21. result.MasterName = "Master";
  22. }
  23. }
  24.  
  25. }
  26. }

2、在Global.asax中注册

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using System.Web.Routing;
  7.  
  8. namespace MvcApplication1
  9. {
  10. // Note: For instructions on enabling IIS6 or IIS7 classic mode,
  11. // visit http://go.microsoft.com/?LinkId=9394801
  12.  
  13. public class MvcApplication : System.Web.HttpApplication
  14. {
  15. public static void RegisterGlobalFilters(GlobalFilterCollection filters)
  16. {
  17.  
  18. filters.Add(new MasterFilterAttribute());
  19.  
  20. }
  21. public static void RegisterRoutes(RouteCollection routes)
  22. {
  23. routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
  24.  
  25. routes.MapRoute(
  26. "Default", // Route name
  27. "{controller}/{action}/{id}", // URL with parameters
  28. new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
  29. );
  30. }
  31.  
  32. protected void Application_Start()
  33. {
  34. AreaRegistration.RegisterAllAreas();
  35.  
  36. RegisterGlobalFilters(GlobalFilters.Filters);
  37. RegisterRoutes(RouteTable.Routes);
  38. }
  39. }
  40. }

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. xampp 通过pear安装PHP_UML

    1.在环境变量中添加D:\xampp\php   // 如果前面已经有其他环境变量了,那么要用分号;跟前面的隔开 2.ctrl+R , cmd , pear install PHP_UML 3.如果提 ...

  2. HDU 6354.Everything Has Changed-简单的计算几何、相交相切圆弧的周长 (2018 Multi-University Training Contest 5 1005)

    6354.Everything Has Changed 就是计算圆弧的周长,总周长=大圆周长+相交(相切)部分的小圆的弧长-覆盖掉的大圆的弧长. 相交部分小圆的弧长直接求出来对应的角就可以,余弦公式, ...

  3. opencv获取像素的值

    opencv中获取图像像素的方法 方法一: IplImage *img = cvLoadImage("Lena.jpg", 0); CvScalar pixel; for (int ...

  4. UVALive - 3211 (2-SAT + 二分)

    layout: post title: 训练指南 UVALive - 3211 (2-SAT + 二分) author: "luowentaoaa" catalog: true m ...

  5. [CTSC2017]密钥

    传送门:http://uoj.ac/problem/297 “无论哪场比赛,都要相信题目是水的” 这不仅是HNOI2018D2T3的教训,也是这题的教训,思维定势真的很可怕. 普及组水题,真是愧对CT ...

  6. [BZOJ 1912] patrol 巡逻

    Link:https://www.lydsy.com/JudgeOnline/problem.php?id=1912 Algorithm: K=0:res=(n-1)*2   每条边恰好走2遍 K=1 ...

  7. 【动态规划】【记忆化搜索】CODEVS 3409 搬运礼物 CodeVS原创

    考虑暴力递归求解的情况: f(i)=min(a(i),f(i-1),f(i-2),...,f(1)) 由于只要参数相同,f()函数的返回值是一样的,因此导致了大量的重复计算,所以我们可以记忆下来. # ...

  8. 【暴力】bzoj3713 [PA2014]Iloczyn

    没什么好说的. #include<cstdio> using namespace std; typedef long long ll; ]; int main() { scanf(]=; ...

  9. Linux GPRS模块问题

    这是一个硬件问题,不过被我这个学软件的给遇到了.很尴尬,纠结了很久. GPRS模块如果没有插上sim卡,开机之后一切正常.一旦插上卡之后开机大约过十秒钟之后会自动关机.并在串口上面打印一下信息: II ...

  10. java的poi技术读取Excel[2003-2007,2010]

    这篇blog主要是讲述java中poi读取excel,而excel的版本包括:2003-2007和2010两个版本, 即excel的后缀名为:xls和xlsx. 读取excel和MySQL相关: ja ...