asp.net - Asp.net Core 2.0 找不到静态文件错误
问题描述
我正在尝试创建一个 ASP.net Core 2.0 Web 应用程序;我已将静态文件放在名为 Content 的文件夹中。
我给出以下路径:
<link href="@Url.Content("../Content/css/style.css")" rel="stylesheet">
加载视图时出现 404 - not found 错误。但是 GET 请求路径是正确的,并且文件存在于同一路径中。这是执行 GET 请求的路径:
http://localhost:49933/Content/css/style.css
我找到了需要我更改 web.config 文件中的设置的解决方案,但 .Net Core 2.0 没有。我用过MVC。web.config 文件对 IIS 不是很重要吗?
解决方案
您应该将要公开的静态文件放在下面wwwroot
,然后相应地引用它们,例如
<link rel="stylesheet" href="~/css/style.css" asp-append-version="true" />
如果要在外部提供静态文件,wwwroot
则需要配置静态文件中间件,如下所示:
public void Configure(IApplicationBuilder app)
{
app.UseStaticFiles(); // For the wwwroot folder
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), "Content")),
RequestPath = "/Content"
});
}
然后,您可以使用与当前类似的标记:
<link href="@Url.Content("~/Content/css/style.css")" rel="stylesheet">
有关详细信息,请参阅在 Web 根目录之外提供文件。
推荐阅读
- docker - docker-compose 文件中的 shell 形式不调用命令 shell?
- c# - 初始化基类:两次声明一个变量?
- laravel - vuejs 和 laravel-vuejs 看不到组件的变化
- pycharm - 如何配置 PyCharm 以解析对嵌入在 Python 全局命名空间中的自定义函数的引用?
- android - 如何在 WebStorm 中运行 Android 虚拟设备
- sql-server-2008 - Delphi - Query.Open 与 Query.ExecSQL
- android - Admob 奖励视频后端身份验证
- vb.net - 根据列表中另一个列表的项目对列表进行排序
- java - 基于 Crc 从 Artifactory 下载工件
- ruby-on-rails - 上传带有活动记录的图像并参考会话模型保存项目