[英]How to configure dotnet core 3 to serve up React SPA while using Http.sys and a URLPrefix?
更改 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>();
});
配置服務:
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" });
});
}
然后配置是:
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";
});
這似乎是實際靜態文件路徑丟失的問題。 在您的 StaticFilesOptions 中,確保您為文件提供程序提供了 index.html 靜態文件的路徑。
spa.Options.DefaultPageStaticFileOptions = new StaticFileOptions
{
FileProvider = new PhysicalFileProvider
(
@"<YourPath>"
)
}
這些選項的更多詳細信息可以在 Microsoft 的文檔中找到。
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-3.1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.