[英].css files in root folder don't load in _Layout in ASP.NET Core MVC web app
I'm fairly new to ASP.NET Core MVC web apps, and stumbled upon a problem with my loading .css
files in my _Layout.cshtml
.我对 ASP.NET Core MVC web 应用程序相当陌生,偶然发现在我的
_Layout.cshtml
中加载.css
文件时出现问题。
My application is a standard MVC app with a SQL database, 3 controllers HomeController
, StudentsController
and CommunitiesController
for each corresponding view folder.我的应用程序是一个标准的 MVC 应用程序,具有 SQL 数据库、3 个控制器
HomeController
、 StudentsController
和CommunitiesController
每个相应的视图文件夹。
The controllers and views for Student
and Community
are generated scaffolded items. Student
和Community
的控制器和视图是生成的脚手架项目。 And in the Views
folder of each of these models are Index.cshtml
along with other CRUD cshtml files just as expected.并且在每个模型的
Views
文件夹中都是Index.cshtml
以及其他 CRUD cshtml 文件,正如预期的那样。
However, the css file referenced in the _Layout.cshtml
only works for Index.cshtml
of each folder - but not in the CRUD views nor the Index with ID parameters so going to Home/Index
or Student/Index
will have the css styling applied, but Student/Edit
, Community/Index/{id}
will not have the css files applied.但是,_Layout.cshtml 中引用的
_Layout.cshtml
8CBA22E28EB17B5F5C6AE2A266AZ 文件仅适用于每个文件夹的Index.cshtml
- 但不适用于 CRUD 视图或具有 ID 参数的索引,因此转到Home/Index
或Student/Index
将应用 css 样式,但是Student/Edit
, Community/Index/{id}
不会应用 css 文件。
Here's my _Layout.cshtml
:这是我的
_Layout.cshtml
:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"] - School Community</title>
<link href="@Url.Content("site.css")" rel="stylesheet" type="text/css" />
<link href="@Url.Content("bootstrap.min.css")" rel="stylesheet" type="text/css" />
</head>
<body>
<! -- the rest of my common layout here -->
<div class="container">
<main role="main" class="pb-3">
@RenderBody()
</main>
</div>
@RenderSection("Scripts", required: false)
</body>
</html>
I have also checked the pages source and confirmed that those 2 <link/>
tags are included, even in the problematic pages.我还检查了页面源并确认包含这 2 个
<link/>
标签,即使在有问题的页面中也是如此。 I run this on localhost and I'm not sure if it is because of this that the bootstrap won't work, but the homepage is stylized as expected.我在 localhost 上运行它,我不确定是否是因为这个原因导致引导程序无法工作,但主页按预期进行了风格化。 Here's also the sample program that my professor provided: http://afrasialab4.azurewebsites.net/ .
这也是我的教授提供的示例程序: http://afrasialab4.azurewebsites.net/ 。 You can refer to this to see the expected styling.
你可以参考这个来查看预期的样式。 The
@RenderSection
at the end is because the CRUD views use JavaScript.最后的
@RenderSection
是因为 CRUD 视图使用 JavaScript。
Thank you for your help.谢谢您的帮助。
Add this code to the views you want to have the same css and layout.将此代码添加到您希望具有相同 css 和布局的视图中。 If the Layout information is different, create another Layout file and enter only the information you want.
如果 Layout 信息不同,请创建另一个 Layout 文件并仅输入您想要的信息。 If you make another Layout, change this code to the name of the New Layout.
如果您制作另一个布局,请将此代码更改为新布局的名称。
@{ ViewData["Title"] = "Index Students"; Layout = "~/Views/Shared/_LayoutName.cshtml"; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.