首页 文章

Asp.Net Core 2.0 Kestrel不提供静态内容

提问于
浏览
4

编辑:添加了红隼

使用IIS express运行Asp.Net Core 2.0应用程序时,静态文件(css,js)按预期提供 . 但是,当通过"dotnet publish -o [targetDirectory]"和dotnet [website.dll]使用命令行/ Kestrel时,Kestrel不会提供任何静态内容 . 利用浏览器F12,我看到Kestrel返回404错误 . 仔细观察,当直接在浏览器中输入文件路径(localhost:5000 / css /cssfile.css)时,文件不会显示,但浏览器似乎重定向到"localhost:5000/cssfile.css"但仍然返回404错误(请注意缺少的/ css /目录) .

我通过Visual Studio 2017创建了这个项目,并为新的MVC Core 2.0应用程序选择了默认值(安装了SDK) .

我已按照步骤here在program.cs和startup.cs文件中启用静态文件 . 这些实现"app.UseStaticFiles();"和".UseContentRoot(Directory.GetCurrentDirectory())" . 通过谷歌找到的文章似乎都没有帮助 . 我已经验证了dotnet将静态内容复制到目标目录 .

我错过了什么?谢谢

// Program.cs
public static IWebHost BuildWebHost(string[] args) => WebHost
   .CreateDefaultBuilder(args)
   .UseStartup<Startup>()
   .Build();

// Startup.cs
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseExceptionHandler("/Error/HandleError");
    app.UseStaticFiles();

    app.UseMvc(routes =>
    {
        routes.MapRoute( name: "default", template: "{controller=User}/{action=Index}/{id?}");
    });
}

7 回答

  • 2

    我无法使用新的ASP.NET Core 2.0项目按照这些步骤重现您的错误;

    • md静态测试

    • cd静态测试

    • dotnet新网站

    • 在wwwroot中添加文件夹 css .

    • wwwroot/css 中添加文件 site.css .

    • Startup.Configure() 方法的开头插入 app.UseStaticFiles(); .

    • dotnet publish -o pubweb

    • cd pubweb

    • dotnet . \ static-test.dll

    • 使用浏览器访问http://localhost:5000/css/site.css .

    dotnet.exe 在我的终端中呈现以下输出:

    Hosting environment: Production
    Content root path: C:\src\static-test\pubweb
    Now listening on: http://localhost:5000
    Application started. Press Ctrl+C to shut down.
    info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
          Request starting HTTP/1.1 GET http://localhost:5000/css/site.css
    info: Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware[2]
          Sending file. Request path: '/css/site.css'. Physical path: 'C:\src\static-test\pubweb\wwwroot\css\site.css'
    

    如您所见,它将成功正确地在子文件夹中提供css文件 . 请尝试上述步骤,并将代码和输出与失败的项目进行比较 . 如果仍然失败,请在上面的 Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware 上附上 RequestPhysical 路径的调试信息 .

  • 0

    我尝试了很多无用的东西,这对我来说是个问题:

    WebHost.CreateDefaultBuilder(args)
    .UseKestrel()
    .UseContentRoot(Directory.GetCurrentDirectory())
    .UseIISIntegration()
    .UseStartup<Startup>()
    .UseWebRoot("E:\\xyz\\wwwroot")
    .UseUrls("http://localhost:5050")
        .Build();
    

    我添加时,服务静态文件开始工作:

    .UseWebRoot("E:\\xyz\\wwwroot")
    

    我在其中一个运行在端口5000上的服务器上发生了冲突,因此我指定从端口5050开始 .

  • 1

    我刚遇到一个非常类似的问题 . 我无法让Kestrel提供任何静态内容;我有一个在.net核心1.1中运行良好的配置,但它在.net core 2.0中不起作用 .

    我通过它调试,静态文件中间件使用的IFileProvider实例是NullFileProvider . 我必须在Startup.cs中执行此操作以获取静态内容:

    app.UseFileServer(
        new FileServerOptions() {
            FileProvider = new PhysicalFileProvider("some_path")
        });
    
  • 8

    经过反复试验后,我意识到一旦我开始使用Kestrel,静态文件web root就会突然映射到bin文件夹而不是开发文件夹 . 要解决此问题,您可以执行以下两项操作之一 .

    • 您可以将要包括的所有静态文件设置为“复制到输出目录”

    • 您可以将根目录重新映射到某个路径:

    .UseKestrel(...)
    .UseWebRoot(@"C:\MyProject\wwwroot\");
    

    我使用我开发的wwwroot文件夹,所以我不必确保将所有新项目添加到Copy to Output Directory .

  • 1

    对我来说,问题是工作目录 . 当我试图用 dotnet /var/www/project/project.dll 启动应用程序时,我没注意到我所在的目录 . 当您以这种方式启动应用程序时,它会自动使用您当前的目录作为工作目录 .

    当我查看另一个项目的 .service 文件时,我意识到了这一点,该项目具有指定的WorkingDirectory:

    ...
    WorkingDirectory=/var/www/project/
    ExecStart=/usr/bin/dotnet /var/www/project/project.dll
    ...
    

    因此,要么确保在运行项目时位于正确的目录中,要么确保在 .service 文件中正确设置了WorkingDirectory .

  • 1

    我使用带有MS模板的asp.net core 2.1 (MVC)创建了一个新项目,然后包含了带有一些动画的css文件 animate.css .

    为了能够使用它,必须在 _Layout.cshtml 中添加 href
    因此,我已经注意到,MS已经"forgotten"添加了环境 "Staging" and "Production" 所以..如果在"Development"环境中添加了href,一切都可以在调试(IIS Express)中正常工作,但它不适用于自我主机(和我假设也没有"the real" IIS),因为这是 not 开发环境和hrefs是在运行时设置的 not .
    因此,我也为"Staging"和"Production"复制了"Development"环境(参见下面的剪辑) .

    <environment include="Development">
            <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.css" />
            <link rel="stylesheet" href="~/css/site.css" />
            <link rel="stylesheet" href="~/css/animate.css">
        </environment>
        <environment exclude="Development">
            <link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/bootstrap/3.3.7/css/bootstrap.min.css"
                  asp-fallback-href="~/lib/bootstrap/dist/css/bootstrap.min.css"
                  asp-fallback-test-class="sr-only" asp-fallback-test-property="position" asp-fallback-test-value="absolute" />
            <link rel="stylesheet" href="~/css/site.min.css" asp-append-version="true" />
        </environment>
    
        @*<environment include="Staging,Production"> -> was missing completely!copy pasted from Development*@
        <environment include="Staging,Production">
            <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.css" />
            <link rel="stylesheet" href="~/css/site.css" />
            <link rel="stylesheet" href="~/css/animate.css">
        </environment>
        <environment exclude="Staging,Production">
            <link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/bootstrap/3.3.7/css/bootstrap.min.css"
                  asp-fallback-href="~/lib/bootstrap/dist/css/bootstrap.min.css"
                  asp-fallback-test-class="sr-only" asp-fallback-test-property="position" asp-fallback-test-value="absolute" />
            <link rel="stylesheet" href="~/css/site.min.css" asp-append-version="true" />
        </environment>
        @*End manually inserted*@
    
  • 12

    请检查以下标签中是否存在这些静态文件 .

    <environment include="Development">
    </environment>
    

    正如您已发布的那样,如果静态文件存在于 include Development 中,那么它们将不会出现在.1443971中 Published 文件夹 . 另外,检查 View Source 并查看这些链接是否存在 .

相关问题