軟件/環境 | 說明 |
---|---|
操做系統 | Windows 10 |
SDK | 2.1.401 |
ASP.NET Core | 2.1.3 |
IDE | Visual Studio Code 1.28 |
瀏覽器 | Chrome 70 |
本篇代碼如下代碼進行調整:https://github.com/ken-io/asp...html
VS Code 自己不提供 ASP.NET Core MVC 視圖引擎(Razor)的智能感知。
幸運的是,VS Code C#擴展 從 1.17.0 版本開始支持Razor視圖引擎的智能感知。git
因此,咱們要將VS Code C#擴展升級到最新版本。github
網頁中每每有通用的佈局,好比導航、底部等等,這些頁面中共用的部分,就須要放在母版頁裏面。
這樣每一個頁面只用關注本頁面要完成的功能/內容便可。提升了開發效率,也下降了公共部分的維護成本。瀏覽器
Razor視圖引擎原生提供了Layout
的概念,做爲視圖佈局的基礎,可讓咱們在視圖中引用另一個視圖做爲該視圖的母版。mvc
在項目根目錄Views
文件夾中建立子目錄Shared
,並在Shared
目錄中建立母版頁 _Layout.cshtml
asp.net
一般公共的Razor視圖文件名都以_開頭
<html> <head> <title>@ViewBag.Title - Ken.Tutorial</title> </head> <body> <h1>Ken.Tutorial</h1> @RenderBody() </body> </html>
@
ViewBag.Title 用於當前應用該模板的視圖自定義標題@
RenderBody(`表示渲染當前應用該母版的視圖,並填充到當前位置。佈局
在/Views/Home
中新建文件Index.cshtml
在頁面中能夠經過如下方式指定母版頁測試
@{ Layout = "_Layout"; }
@{ Layout = " /Views/Shared/_Layout.cshtml"; }
以上兩種方式任選其一便可spa
@{ Layout = "_Layout"; } <h3>@ViewBag.Title</h3> @ViewBag.Message
調整 HomeController.cs
中Action:Index()
,經過視圖輸出Message操作系統
public IActionResult Index() { ViewBag.Title = "Home"; ViewBag.Message = "Hello World ! -ken.io"; return View(); }
啓動項目,訪問 /
或者 /home/index
將會看到:
經過母版頁,咱們能夠方便的共用一些頁面內容或者功能。可是對於一些特殊的子頁面可能須要重寫母版頁中一些內容,或者在母版頁中插入本身想呈現的內容,而不是隻能將子頁面呈如今固定的位置。
Razor視圖引擎提供了Section
的概念,咱們能夠在視圖中定義Section
,而後再母版視圖中經過RenderSection
方式加載視圖定義的Section
。
Section定義在子頁面纔有效。
Section定義示例:
@section test{ <p>Section Content</p> }
@
section:定義Section的關鍵字
test:SectionName,命名規則同C#變量名同樣,字母或下劃線開頭後面能夠跟字母、下劃線、數字
在母版頁中能夠經過@RenderSection()
方法加載子頁面中定義的Section
RenderSection只有在母版頁(Layout)中使用纔有效
@RenderSection("test")
@RenderSection("test", false)
@if(IsSectionDefined("test")) { RenderSection("test"); } else { <p>Layout Content</p> }
在Controllers
文件夾中建立LayoutController.cs
using System; using Microsoft.AspNetCore.Mvc; namespace Ken.Tutorial.Web.Controllers { public class LayoutController : Controller { public IActionResult SectionDemo() { return View(); } } }
在Views
文件夾中建立Layout
文件夾並建立視圖文件:SectionDemo.cshtml
@{ Layout = "_Layout"; ViewBag.Title = "SectionDemo"; } <h3>@ViewBag.Title</h3> <p>Section Demo by ken.io</p> @section footer{ <p>Section Footer</p> }
修改 _Layout.cshtml
增長Section加載
<html> <head> <title>@ViewBag.Title - Ken.Tutorial</title> </head> <body> <div class="header"> <h1>Ken.Tutorial</h1> <hr/> </div> <div class="content"> @RenderBody() </div> <div class="footer"> <hr/> @if(IsSectionDefined("footer")) { RenderSection("footer"); } else { <p>Layout Footer</p> } </div> </body> </html>
啓動項目,經過瀏覽器進行訪問測試:/
,/layout/sectiondemo
訪問 /
,將看到:
訪問/layout/sectiondemo
將看到:
Razor視圖引擎,提供了在視圖呈現以前執行代碼的入口。
這個入口是一個約定的文件即:_ViewStart.cshtml
,咱們能夠經過該文件定義全局視圖呈現前執行的代碼,也是定義某個文件夾下的視圖呈現前須要執行的代碼。
完整路徑示例:
若是兩個_ViewStart.cshtml
文件同時存在,那麼/Views/_ViewStart.cshtml
的執行優先級高於/Views/Home/_ViewStart.cshtml
在Views
文件夾下建立視圖文件_ViewStart.cshtml
@{ Layout = "_Layout"; }
這裏咱們經過全局代碼,將全部視圖的母版頁都指定爲_Layout
。
這樣咱們在視圖子頁面就不用逐一制定母版頁了。
若是咱們將Index.cshtml
中指定的Layout註釋掉
@{ //Layout = "_Layout"; }
而後啓動項目,訪問 /
,依然看到:
在/Views/Home
文件夾下建立視圖文件_ViewStart.cshtml
@{ Layout = null; }
這裏咱們局部全局代碼,將在/Views/Home
文件夾下的全部視圖的母版頁都指定爲null
,默認不引用任何母版頁。
這時咱們啓動項目,訪問 /
,將看到:
https://github.com/ken-io/asp...
https://docs.microsoft.com/zh...
本文首發於個人獨立博客:https://ken.io/note/asp.net-c...