我正在尝试将 css 文件添加到基本 html 文件(与 css 文件一起在/dashboard 中),但它不起作用:
@{
ViewBag.Title = "Index";
}
<head>
<link rel="stylesheet" href="~/Views/Dashboard/Dashboard.css" type="text/css" />
</head>
<body>
<h2 id="yes">Index</h2>
<h1>@ViewBag.test</h1>
</body>
我尝试了多种方法,包括将文件添加到目录文件夹,并添加
@Styles.Render("~/Content/css")
这是语法问题吗?或者对布局系统的工作原理缺乏了解?仍然围绕着 asp.net 和 MVC...
最佳答案
您不应该在 @section Scripts
下加载 CSS .首先加载 CSS 是有原因的!
例如,查看 Patrick Sexton 的引用资料:Call CSS First
Ensuring the calls for CSS files come first helps ensure the browser gets it first. This saves time by reducing network calls and not letting javascript activity delay the browser from getting CSS
真正的解决方案——首先加载你的 CSS
步骤#1
如果你想根据 .cshtml
加载不同的 CSS 文件文件,并假设它们都使用相同的 _Layout.cshtml
,你可以这样做:
在您的 _Layout.cshtml
上, 在你的 <head>
之间HTML 标签,这样写:
@RenderSection("Stylesheets", required: true)
将其放入您的 <head>
标签将确保它首先被加载!
注意:required: true
部分是可选的。
第二步
然后,在您的 .cshtml
上 View ,在顶部添加:
@section Stylesheets {
}
在里面,加载你的 <link>
标签。
关于asp.net - 将css文件添加到cshtml文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42589395/