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

Abh*_*hna 4 .net asp.net asp.net-core-mvc asp.net-core asp.net-core-2.0

大家好我想创建一个ASP.net Core 2.0 Web应用程序; 我已将静态文件放在名为Content的文件夹中.

在此输入图像描述

我给出以下路径:

<link href="@Url.Content("../Content/css/style.css")" rel="stylesheet">
Run Code Online (Sandbox Code Playgroud)

我在加载视图时收到404 - 未找到错误.但是,GET请求路径是正确的,并且该文件存在于同一路径中.这是GET请求的完成路径:

http://localhost:49933/Content/css/style.css 
Run Code Online (Sandbox Code Playgroud)

我发现需要我更改web.config文件中的设置的解决方案,但.Net Core 2.0没有.我用过MVC.对于IIS,web.config文件不是很重要吗?

Cal*_*alC 11

您应该放置您希望公开的静态文件wwwroot,然后相应地引用它们,例如

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

<link rel="stylesheet" href="~/css/style.css" asp-append-version="true" />
Run Code Online (Sandbox Code Playgroud)

如果要在外部提供静态文件,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"
    });
}
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用与目前相似的标记:

<link href="@Url.Content("~/Content/css/style.css")" rel="stylesheet">
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅Web根目录之外的服务文件.