一种在MVC3框架里面设置模板页的方法,不使用_ViewStart
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、創(chuàng)建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、創(chuàng)建子頁面Home,Views\Home\Home.cshtml
@{
??? ViewBag.Title = "Index";
}
?
<h2>Index</h2>
?
5、效果圖
?
?
。
轉(zhuǎn)載于:https://www.cnblogs.com/xiaochun126/p/4778946.html
總結
以上是生活随笔為你收集整理的一种在MVC3框架里面设置模板页的方法,不使用_ViewStart的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言,gdb
- 下一篇: CPlus的简单线程的制作