首页 > 解决方案 > 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 不是很重要吗?

标签: asp.net.netasp.net-coreasp.net-core-mvcasp.net-core-2.0

解决方案


您应该将要公开的静态文件放在下面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 根目录之外提供文件


推荐阅读