为什么 Asp net Core 针对我的 Angular Spa dist 文件的请求返回 index.html?

Jor*_*anM 3 iis url-rewriting kestrel asp.net-core angular

将我的 Asp Net Core Angular Spa 部署到共享 Web 服务器后,对 Angular 运行时组件的请求将返回我的 index.html 文件。

编辑(重新表述问题以使其清晰):

我有一个有角度的水疗中心,由 asp net core 角度模板提供服务。当我导航到应用程序的 url 时,会返回索引页面,然后返回客户端应用程序的脚本文件的后续调用也会返回 index.html...即使该 url 正在请求 runtime.js、main.js 文件。 js等

除了下面我的代码摘录之外,还需要什么额外的配置?

public void ConfigureServices(IServiceCollection services)
{
    services.AddSpaStaticFiles(configuration =>
    {
        configuration.RootPath = "ClientApp/dist";
    });
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseStaticFiles();
    app.UseSpaStaticFiles();
    app.UseMvc();

    app.UseSpa(spa =>
    {
        spa.Options.SourcePath = "ClientApp";
    }); 
}
Run Code Online (Sandbox Code Playgroud)

在我的 angular.json 中,我指定了以下 URL 前缀:

"baseHref": "/APPNAME/",
"deployUrl": "/APPNAME/ClientApp/dist/"
Run Code Online (Sandbox Code Playgroud)

源树似乎是正确的。

对每个运行时组件的请求都是成功的,但它们都返回我的index.html,而不是请求的内容。

请求网址:https ://example.com/MYAPP/ClientApp/dist/main.17cf96a7a0252eeecd9e.js

Chrome 网络调试器的响应:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>APP NAME</title>

  <base href="/APPNAME/">

  <meta content="width=device-width, initial-scale=1"
        name="viewport">
  <link href="HRDRicon.png"
        rel="icon"
        type="image/x-icon">
<link rel="stylesheet" href="/APPNAME/ClientApp/dist/styles.d9e374eff45177231bee.css"></head>
<body class="fixed-sn white-skin">

<app-root></app-root>

<noscript>
  <p>
    Sorry, JavaScript must be enabled to use this app
  </p>
</noscript>

<script type="text/javascript" src="/APPNAME/ClientApp/dist/runtime.e460f84ccfdac0ca4695.js">
</script><script type="text/javascript" src="/APPNAME/ClientApp/dist/polyfills.d0cb8e9b276e2da96ffb.js"></script>
<script type="text/javascript" src="/APPNAME/ClientApp/dist/scripts.a54ea6f0498a1f421af9.js"></script>
<script type="text/javascript" src="/APPNAME/ClientApp/dist/main.17cf96a7a0252eeecd9e.js"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

我将在评论中添加图像的链接,因为这在原始帖子中受到限制。

Jör*_*zen 5

我也为水疗扩展而苦苦挣扎。似乎UseSpa对 index.html 设置了一个包罗万象的规则,该规则还拦截对所有静态文件(如 .js 和 .css)的请求。为了让我的 spa 在子目录中工作,所需要做的就是在startup.cs 中进行修改 1. 和 2.。无需水疗扩展。

public static void Configure( IApplicationBuilder app, IWebHostEnvironment env)
{
    // ...
    app.UseHttpsRedirection();
    app.UseStaticFiles();

    // 1. Configure request path for app static files
    var fileExtensionProvider = new FileExtensionContentTypeProvider();
    fileExtensionProvider.Mappings[".webmanifest"] = "application/manifest+json";
    app.UseStaticFiles(new StaticFileOptions()
    {
        ContentTypeProvider = fileExtensionProvider,
        RequestPath = "/myapp",
        FileProvider = new PhysicalFileProvider(
            Path.Combine(Directory.GetCurrentDirectory(), @"MyApp/dist"))
    });

    app.UseRouting();

    app.UseAuthentication();
    app.UseAuthorization(); 

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapDefaultControllerRoute();
        endpoints.MapRazorPages();
        // 2. Setup fallback to index.html for all app-routes
        endpoints.MapFallbackToFile( @"/myapp/{*path:nonfile}", @"MyApp/dist/index.html");
    });
}
Run Code Online (Sandbox Code Playgroud)