【问题标题】:Asp.net Core 2.0 Static files not found errorAsp.net Core 2.0 找不到静态文件错误
【发布时间】:2018-09-19 12:02:08
【问题描述】:

我正在尝试创建一个 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 .net asp.net-core asp.net-core-mvc asp.net-core-2.0


    【解决方案1】:

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

    见Static files in 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">
    

    更多信息请参见Serve files outside of web root。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-28
      • 2018-02-18
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      • 2019-10-26
      相关资源
      最近更新 更多