[英]How to configure dotnet core 3 to serve up React SPA while using Http.sys and a URLPrefix?
After changing the URLPrefix I get the following error:更改 URLPrefix 后,出现以下错误:
The SPA default page middleware could not return the default page '/index.html' because it was not found, and no other middleware handled the request. SPA 默认页面中间件无法返回默认页面“/index.html”,因为未找到它,并且没有其他中间件处理该请求。
Something thus is required to tell dotnet core about the prefix but I can't seem to find the right combination of settings.因此需要一些东西来告诉 dotnet core 有关前缀的信息,但我似乎无法找到正确的设置组合。
Help much appreciated.非常感谢帮助。
The code is below:代码如下:
HostBuilder is setup with: 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" });
});
}
And then Configure is:然后配置是:
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";
});
This appears to be an issue where the path to the actual static files is lost.这似乎是实际静态文件路径丢失的问题。 In your StaticFilesOptions ensure that you are providing a File provider with the path to your index.html static files.在您的 StaticFilesOptions 中,确保您为文件提供程序提供了 index.html 静态文件的路径。
spa.Options.DefaultPageStaticFileOptions = new StaticFileOptions
{
FileProvider = new PhysicalFileProvider
(
@"<YourPath>"
)
}
Further details for these options can be found in Microsoft's documentation.这些选项的更多详细信息可以在 Microsoft 的文档中找到。
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-3.1 https://docs.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-3.1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.