英文:
ASP.NET Core 6 MVC not loading CSS styles
问题
在我的ASP.NET Core 6 MVC项目中,.css
文件在视图文件中无法正常工作。
我将.NET作为HTML文件在外部运行,它可以正常工作。
英文:
In my ASP.NET Core 6 MVC project, the .css
file is not working in the view file.
I ran .NET externally as html file and it worked without any problems.
答案1
得分: 2
在我的ASP.NET Core 6 MVC项目中,视图文件中的.css文件无法正常工作。
正如您所知,静态文件存储在项目的Web根目录中,默认目录是{content root}/wwwroot
,您可以参考官方文档这里关于静态文件的信息。
在wwwroot目录下:
因此,您需要将CSS文件放在wwwroot目录下的文件夹中,如下所示:
[
然后尝试使用:
<link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />
在Program.cs文件中包括以下引用:
app.UseStaticFiles();
在wwwroot之外:
此外,如果您想在web根目录之外提供文件,考虑以下目录层次结构:
Program.cs
在您的program.cs文件中包括以下引用:
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(builder.Environment.ContentRootPath, "StaticFilesFolderName")),
RequestPath = "/StaticFilesFolderName"
});
使用引用:
<link rel="stylesheet" href="~/StaticFilesFolderName/site.css" asp-append-version="true" />
注意:
根据您的情况,这可能是AdminLTE... 所以请按照准确的路径进行操作。有关更多详细信息和示例,请查看我们的官方文档。
英文:
> In my ASP.NET Core 6 MVC project, the .css file is not working in the
> view file.
As you may know, Static files are stored within the project's web root directory.And the default directory is {content root}/wwwroot
, You can refer to the official document here about static files.
Inside wwwroot:
Thus, You need to put your css files either in folder into wwwroot like following:
Then try to use:
<link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />
In Program.cs file include the reference:
app.UseStaticFiles();
Outside wwwroot:
Furthermore, if you want to serve files outside of the web root in that scenario, Consider below directory hierarchy :
Program.cs
Include reference as following in your program.cs file:
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(builder.Environment.ContentRootPath, "StaticFilesFolderName")),
RequestPath = "/StaticFilesFolderName"
});
Use Reference:
<link rel="stylesheet" href="~/StaticFilesFolderName/site.css" asp-append-version="true" />
Note:
As per your scenario, it would be AdminLTE... so on and follow the accurate path. For more details and example please have a look our official document here.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论