日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

一种在MVC3框架里面设置模板页的方法,不使用_ViewStart

發布時間:2025/3/20 c/c++ 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 一种在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、創建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、效果圖

?

?

轉載于:https://www.cnblogs.com/xiaochun126/p/4778946.html

總結

以上是生活随笔為你收集整理的一种在MVC3框架里面设置模板页的方法,不使用_ViewStart的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。