html - 如何仅添加部分 View 所需的脚本和 css 文件

标签 html css asp.net-mvc

我创建了一些使用 _layout 部分 View 的 View ,但我不想包含每个 css 或脚本包。在 View 中包含脚本或 css 文件的最佳方法是什么?

<head>
 <link href="Content/style.css" .../>
 <script src="Scripts/myscript.js" ...></script>
</head>

如何让 View 呈现与此类似的内容?使用部分 View 的 View 中没有 head 标签。

最佳答案

使用 Razor 布局,您可以使用部分来定义您希望在部分而不是主布局文件中加载的脚本/CSS 文件。

您可以阅读有关部分 here, on Scott Guthrie's blog 的更多信息

在您共享的 _Layout.cshtml 文件中,您可以添加

<head>
    <meta charset="utf-8" />
    <title>@ViewBag.Title</title>
    @RenderSection("css")
    @RenderSection("scripts")
</head>

在特定的 View 中...

<div> lol some content </div>
@section css {
   <link rel="stylesheet" type="text/css" href="homeTheme.css">
}
@section scripts {
   <script src="../../Scripts/home.js"></script>
}

关于html - 如何仅添加部分 View 所需的脚本和 css 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24958602/

相关文章:

javascript - 使 div 及其所有子项忽略父类样式

javascript - 为什么第 nth-child 将样式添加到备用可见元素不起作用?

c# - 如何为动态创建的文本框和 div 设置随机 id

jquery - 将选项卡的值重置为默认 css 状态

asp.net-mvc - MVC Controller 设计

python - 抓取亚马逊评论,不能排除付费评论

css - IE8 和其他所有内容之间的 CSS 旋转的一致来源?

css - 为什么设置 box-sizing : content-box; on a singular element?

asp.net-mvc - MVC Ajax.BeginForm 替换奇怪的行为

mysql - EF6 与 MySQL。字典中不存在给定的键