基于子页面

时间:2017-01-22 08:15:14

标签: asp.net asp.net-mvc master-pages

在MVC中,可以在_Layout.cshtml中使用:

@if (IsSectionDefined("styles"))
{@RenderSection("styles", required: false)}
如果需要给定部分,

与内容页面/布局上的@Section styles一起在<head>中包含css样式。

是否有一种模仿Web窗体中相同行为的有效方法?

总结一下:我希望有可能在母版页上呈现css和js,只有当基于该母版页的特定子页面需要它们时才会这样。所以它不会包含在不需要那些css和js文件的页面上。

1 个答案:

答案 0 :(得分:1)

您可以使用ContentPlaceHolder。在页面中,按如下方式定义内容占位符:

<head runat="server">
    <title>..</title>
    <meta charset="utf-8"/>
    ..
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>

..那些需要特殊cssjs文件的子页面可以使用此内容占位符来注入所需的文件,例如:

<%@ Page Title="" Language="C#" MasterPageFile=".." AutoEventWireup="true" CodeBehind=".." %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <link href="some css file dedicated only to this child page" rel="stylesheet" />
    <script src="..js.."></script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    ..
    <h1><asp:Label ID="lblPageTitle" runat="server" Text=""></asp:Label></h1>
    ..
</asp:Content>