简体   繁体   English

.css 根文件夹中的文件不会加载到 ASP.NET Core MVC web 应用程序中的 _Layout 中

[英].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 个控制器HomeControllerStudentsControllerCommunitiesController每个相应的视图文件夹。

The controllers and views for Student and Community are generated scaffolded items. StudentCommunity的控制器和视图是生成的脚手架项目。 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/IndexStudent/Index将应用 css 样式,但是Student/EditCommunity/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.

相关问题 Asp.net core 3.0 - 静态文件不加载 - Asp.net core 3.0 - static files don't load ASP.NET Core使用FTP时写入和删除Web根目录下的文件 - Write and Delete Files in Web Root Folder When Using FTP in ASP.NET Core asp.net 核心 3.1 中的 css 和 js 文件不工作 - don`t work css and js files in asp.net core 3.1 使用 ASP.NET Core MVC 将带有文件的数据发布到 api (ASP.NET Core Web API) - Post data with files using ASP.NET Core MVC to api (ASP.NET Core Web API) 在 ASP.NET Core 3 MVC Web 应用程序中设置路由 - Setting up routes in ASP.NET Core 3 MVC web app 在 ASP.NET Core MVC Web 应用程序中出现构造函数错误? - Getting constructor error in ASP.NET Core MVC web app? 运行时出现 InvalidOperationException ASP.NET Core MVC Web App - InvalidOperationException while running ASP.NET Core MVC Web App AuthenticationStateProvider 无法注入 ASP.NET Core Web App - AuthenticationStateProvider couldn't inject in ASP.NET Core Web App 在 ASP.NET Core MVC 应用程序的布局布局页面中存储个人资料图片、姓名、职位 - Storing profile picture, name, Job Title in layout layout Page of ASP.NET Core MVC app 无法在Content&Script文件夹中获取任何文件以在VS中的C#ASP.NET MVC应用程序中加载 - Can't get any files in the Content & Script folder to load in C# ASP.NET MVC application in VS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM