如何在使用Http.sys和URLPrefix时配置dotnet core 3来提供React SPA?


9

更改URLPrefix后,出现以下错误:

SPA默认页面中间件无法返回默认页面'/index.html',因为找不到默认页面,并且没有其他中间件处理该请求。

因此,需要一些信息来告诉dotnet core有关前缀的信息,但是我似乎找不到正确的设置组合。

帮助非常感谢。

代码如下:

HostBuilder的设置:

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
    .ConfigureWebHostDefaults(webBuilder =>
    {
        webBuilder.UseHttpSys(options =>
        {
            options.AllowSynchronousIO = false;
            options.Authentication.Schemes = AuthenticationSchemes.None;
            options.Authentication.AllowAnonymous = true;
            options.MaxConnections = null;
            options.MaxRequestBodySize = 30000000;
            options.UrlPrefixes.Add("http://localhost:5005/Product/Site");
        });
        webBuilder.UseStartup<Startup>();
    });

ConfigureServices:

public override void ConfigureServices(IServiceCollection services)
{
  services.AddRazorPages();

  services.AddSpaStaticFiles(configuration =>
  {
    configuration.RootPath = "ClientApp/build";
  });

  services.AddMvc();
  services.AddResponseCompression(opts =>
  {
    opts.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(
              new[] { "application/octet-stream" });
  });
}

然后Configure是:

      app.UseSpaStaticFiles();
      app.UseRouting();
      app.UseEndpoints
      (
        endpoints =>
        {
          endpoints.MapControllerRoute(
              name: "default",
              pattern: "{controller}/{action=Index}/{id?}");
        }
      );

      app.UseSpa(spa =>
      {
        //spa.Options.DefaultPage = reactPath + "/index.html";
        spa.Options.DefaultPage = "/index.html";

        spa.Options.SourcePath = "ClientApp";


      });

Answers:


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.