我已经创建了一个 asp.net core Web 应用程序(我从“空”选项开始),并且正在逐步构建它。看来我的 css 文件没有被读取或找到。
当我启动应用程序时,我可以看到我的 html 页面看起来不正常,当我在控制台下的 Edge 中使用开发人员工具时,会出现一个HTTP404: NOT FOUND - The server has not found anything matching the requested URI (Uniform Resource Identifier). GET - http://localhost:8888/css/site.css
错误表明我的 css 文件确实没有被读取或找到。
现在,我的 css 文件位于项目根目录中名为“css”的文件夹下。当我说 root 时,我的意思是我刚刚创建了一个名为 css 的文件夹,并且路径与 Program.cs 或 Startup.cs 相同。我的项目中没有wwwroot(好像asp.net core 2.2默认已经去掉了?),css文件夹也不在。如下是我的 html 页面中的代码,我尝试在“/css”之前添加 ~ 但仍然没有。
<head>
<link href="/css/site.css" rel="stylesheet" />
</head>
如果您希望在 Web 根目录之外提供静态文件(例如在名为的文件夹中)MyStaticFiles
)并且它的结构如下
-YourProject
--MyStaticFiles
---css
----site.css
--Program.cs
--Startup.cs
你可以使用app.UseStaticFiles
,参考在 Web 根目录之外提供文件服务 https://learn.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-2.2#serve-files-outside-of-web-root
public void Configure(IApplicationBuilder app)
{
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), "MyStaticFiles")),
RequestPath = "/StaticFiles"
});
}
然后你可以从http://localhost:8888/StaticFiles/css/site.css
or <link rel="stylesheet" href="~/StaticFiles/css/site.css" />
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)