ASP.Net Core app.UseStaticFiles在生产环境(Azure)中不起作用,对静态文件的调用将降至默认路由

时间:2019-05-18 03:53:49

标签: asp.net azure asp.net-core

我有一个也使用Angular的ASP.Net Core应用程序。在我的本地计算机上,它能够提供静态文件,例如js和图像。部署到Azure时,对静态文件的调用将返回默认路由(它将检索Angular应用程序默认页面。)

在我的Startup.cs中,我使用“ app.UseStaticFiles”。这在本地有效。

找到我的Startup.cs here

这可能是Azure的配置问题吗?还是开发环境中存在但生产中不存在的某些逻辑?

4 个答案:

答案 0 :(得分:0)

您的配置丢失了,您需要在ConfigureServices的末尾添加它

 // In production, the Angular files will be served from this directory
 services.AddSpaStaticFiles(configuration =>
 {
    configuration.RootPath = "ClientApp/dist";
  });

然后在“配置”中确保管道的顺序正确,例如

app.UseStaticFiles();
app.UseSpaStaticFiles();

//put this at the end of Configure method
app.UseSpa(spa =>
{
   // To learn more about options for serving an Angular SPA from ASP.NET Core,
   // see https://go.microsoft.com/fwlink/?linkid=864501

   spa.Options.SourcePath = "ClientApp";

   if (env.IsDevelopment())
   {
      spa.UseAngularCliServer(npmScript: "start");
   }
});

答案 1 :(得分:0)

对于遇到这个问题(实际上是UseStaticFiles在启动时)的其他人,我的问题很简单:我的子文件夹文件未标记为CopyAlways(输出文件夹)。

答案 2 :(得分:0)

仅遇到此问题,问题是Linux主机上的ClientApp / dist文件夹没有正确的权限。

确保ClientApp / dist文件夹具有正确的权限,以便.NET Core进程可以正确访问该文件夹。就我而言,我缺少“输入”权限,因此我的应用程序退回到了wwwroot文件夹。

我运行以下命令来正确设置应用程序根目录中所有目录的权限。

find /path/to/your/app -type d -exec chmod 755 {} \;

答案 3 :(得分:0)

我有以下检查清单:

  • 检查静态文件属性“复制到输出目录”设置为“始终复制”。

  • 检查 app.UseStaticFiles() 已在 Startup.cs 的 Configure() 方法中调用

  • 对于 Linux:调用方法 UseStaticFiles() 为:

     app.UseStaticFiles(new StaticFileOptions() {
         FileProvider =  new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot"))
     });