如何在使用 Http.sys 和 URLPrefix 时配置 dotnet core 3 以提供 React SPA?
How to configure dotnet core 3 to serve up React SPA while using Http.sys and a URLPrefix?
更改 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.
因此需要一些东西来告诉 dotnet 核心有关前缀的信息,但我似乎找不到正确的设置组合。
非常感谢帮助。
代码如下:
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" });
});
}
然后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";
});
这似乎是实际静态文件路径丢失的问题。在您的 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
更改 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.
因此需要一些东西来告诉 dotnet 核心有关前缀的信息,但我似乎找不到正确的设置组合。
非常感谢帮助。
代码如下:
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" });
});
}
然后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";
});
这似乎是实际静态文件路径丢失的问题。在您的 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