将 CSS 或 JavaScript 文件添加到视图或部分视图的布局头 [英] Add CSS or JavaScript files to layout head from views or partial views
本文介绍了将 CSS 或 JavaScript 文件添加到视图或部分视图的布局头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
布局页面头部:
<head>
<link href="@Url.Content("~/Content/themes/base/Site.css")"
rel="stylesheet" type="text/css" />
</head>
应用程序需要一个视图(AnotherView):
A View (AnotherView) from the application needs:
<link href="@Url.Content("~/Content/themes/base/AnotherPage.css")"
rel="stylesheet" type="text/css" />
而另一个视图有一个局部视图(AnotherPartial),它需要:
and AnotherView has a partial view (AnotherPartial) which needs:
<link href="@Url.Content("~/Content/themes/base/AnotherPartial.css")"
rel="stylesheet" type="text/css" />
问题:我们如何添加这些CSS文件链接AnotherView和AnotherPartial链接到布局头?
Question: How can we add these CSS files links AnotherView and AnotherPartial links to Layout head?
RenderSection 不是一个好主意,因为 AnotherPage 可以有多个 Partials.将所有 CSS 添加到 head 没有用,因为它会动态更改(取决于另一个页面).
RenderSection is not a good idea because AnotherPage can have more than one Partials. Add all CSS to head is not useful because it will change dynamicaly (it depends on Anotherpages).
推荐答案
布局:
<html>
<head>
<meta charset="utf-8" />
<title>@ViewBag.Title</title>
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/jquery-1.6.2.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/modernizr-2.0.6-development-only.js")" type="text/javascript"></script>
@if (IsSectionDefined("AddToHead"))
{
@RenderSection("AddToHead", required: false)
}
@RenderSection("AddToHeadAnotherWay", required: false)
</head>
查看:
@model ProjectsExt.Models.DirectoryObject
@section AddToHead{
<link href="@Url.Content("~/Content/Upload.css")" rel="stylesheet" type="text/css" />
}
这篇关于将 CSS 或 JavaScript 文件添加到视图或部分视图的布局头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文