zoukankan      html  css  js  c++  java
  • ASP.NET MVC 在子页中引用头文件

    在很多时候我们把网站公共的js、css文件放在模板页中,这样在具体的每一个页面里面就不需要单独引用。

    ASP.NET WebForm里面使用.site文件。

    而在ASP.NET MVC 中使用了类似下面的定义

    _Layout.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.8.0.min.js")" type="text/javascript"></script>
    </head>
    <body>
        @RenderBody()
    </body>
    </html>

    这样在子页中的代码就直接进入html的body中了。

    但是有时候单独的页面有单独的头文件,不需要在模板页中引用。那此时需要用如下的方法了:

    _Layout.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.8.0.min.js")" type="text/javascript"></script>
        @RenderSection("Head", required: false)
    </head>
    <body>
        @RenderBody()
    </body>
    </html>

    注意在head节点中增加了

    @RenderSection("Head", required: false)

    这样在对应的页面中可以使用如下代码进行引用

    @section Head{
        <link href="@Url.Content("~/Content/css/datebox.css")" rel="stylesheet" type="text/css" />
        <link href="@Url.Content("~/Content/css/easyui.css")" rel="stylesheet" type="text/css" />
        <script src="@Url.Content("~/Scripts/jquery.easyui.min.js")" type="text/javascript"></script>
        }

    注意:如果在head节点中定义的required值为true,则必须要为每一个使用模板页的页面添加引用代码,如果required值为false,则可以增加也可以不增加。

  • 相关阅读:
    ls 按大小排序 按时间排序
    【u035】奶牛的电信
    【record】#10
    【record】10.30..11.6
    【33.33%】【codeforces 608C】Chain Reaction
    【44.19%】【codeforces 608D】Zuma
    【22.73%】【codeforces 606D】Lazy Student
    【27.40%】【codeforces 599D】Spongebob and Squares
    【26.67%】【codeforces 596C】Wilbur and Points
    【13.91%】【codeforces 593D】Happy Tree Party
  • 原文地址:https://www.cnblogs.com/smallerpig/p/3646158.html
Copyright © 2011-2022 走看看