繁体   English   中英

Asp.net Core 2.0 未找到静态文件错误

[英]Asp.net Core 2.0 Static files not found error

我正在尝试创建一个 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下,然后相应地引用它们,例如

请参阅ASP.NET Core 中的静态文件

<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 根目录之外提供文件

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM