ASP.NET Core 6 MVC未加载CSS样式。

huangapple go评论126阅读模式
英文:

ASP.NET Core 6 MVC not loading CSS styles

问题

在我的ASP.NET Core 6 MVC项目中,.css文件在视图文件中无法正常工作。

我将.NET作为HTML文件在外部运行,它可以正常工作。

ASP.NET Core 6 MVC未加载CSS样式。

英文:

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.

ASP.NET Core 6 MVC未加载CSS样式。

答案1

得分: 2

在我的ASP.NET Core 6 MVC项目中,视图文件中的.css文件无法正常工作。

正如您所知,静态文件存储在项目的Web根目录中,默认目录是{content root}/wwwroot,您可以参考官方文档这里关于静态文件的信息。

在wwwroot目录下:

因此,您需要将CSS文件放在wwwroot目录下的文件夹中,如下所示:

[ASP.NET Core 6 MVC未加载CSS样式。

然后尝试使用:

<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:

ASP.NET Core 6 MVC未加载CSS样式。

Then try to use:

  &lt;link rel=&quot;stylesheet&quot; href=&quot;~/css/site.css&quot; asp-append-version=&quot;true&quot; /&gt;

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, &quot;StaticFilesFolderName&quot;)),
    RequestPath = &quot;/StaticFilesFolderName&quot;
});

Use Reference:

&lt;link rel=&quot;stylesheet&quot; href=&quot;~/StaticFilesFolderName/site.css&quot; asp-append-version=&quot;true&quot; /&gt;

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.

huangapple
  • 本文由 发表于 2023年2月8日 22:31:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75387270.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定