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

新手上路,请多包涵

使用 IIS express 运行 Asp.Net Core 2.0(或 2.2)应用程序时,静态文件(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)选择了默认值。

我已按照 此处 的步骤在 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?}");
    });
}

原文由 davewilliams459 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.1k
2 个回答

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

  1. md 静态测试
  2. cd 静态测试
  3. 点网新网
  4. 在 wwwroot 中添加文件夹 css
  5. 在 --- 中添加文件 site.css wwwroot/css
  6. Startup.Configure() 方法的开头插入 app.UseStaticFiles();
  7. dotnet 发布 -o pubweb
  8. cd pubweb
  9. dotnet .\static-test.dll
  10. 使用浏览器访问 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 文件。请尝试上述步骤,并将代码和输出与您失败的项目进行比较。如果仍然失败,请在 RequestPhysical 来自 Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware --- 的路径上附加调试信息。

原文由 joakimriedel 发布,翻译遵循 CC BY-SA 3.0 许可协议

在 .NET Core 2.2 MVC 中遇到了同样的问题,将其作为具有“本地系统”用户的独立 Windows 服务运行。

问题是windows服务使用“C:\Windows\System32”中的默认目录,所以你需要定义新路径:

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

这是我用于在运行时检测路径的代码:

 // Find app directory.
var isService = !(Debugger.IsAttached || args.Contains("--console"));
var currentDir = Directory.GetCurrentDirectory();
if (isService)
{
    var pathToExe = Process.GetCurrentProcess().MainModule.FileName;
    currentDir = Path.GetDirectoryName(pathToExe);
}

// Configure web host
var host = WebHost.CreateDefaultBuilder(args)
        .UseKestrel()
        .UseContentRoot(currentDir)
        .UseWebRoot(Path.Combine(currentDir, "wwwroot"));
        .UseStartup<Startup>()
        .Build();

// Run.
if (isService)
{
    host.RunAsService();
}
else
{
    host.Run();
}

原文由 luka_matkic 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题