reactjs - .Net Core 3.1 SPA React 应用程序不提供 Hangfire 仪表板/Swagger 在生产中
问题描述
从.NET Core 3.1安装 React 应用程序模板后,使用dotnet new react
该应用程序在开发和生产中完美运行。尝试使用 Swagger 或 Hangfire 仪表板端点时会出现问题。
创建应用程序后,我添加了 Hangfire 的包引用,并出于实际目的添加了内存存储:
<PackageReference Include="Hangfire" Version="1.7.*" />
<PackageReference Include="Hangfire.MemoryStorage" Version="1.7.0" />
在Startup.cs
:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddHangfire(config =>
config.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
.UseSimpleAssemblyNameTypeSerializer()
.UseDefaultTypeSerializer()
.UseMemoryStorage());
services.AddHangfireServer();
// In production, the React files will be served from this directory
services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "ClientApp/build";
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseSpaStaticFiles();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapHangfireDashboard();
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller}/{action=Index}/{id?}");
});
app.UseSpa(spa =>
{
spa.Options.SourcePath = "ClientApp";
if (env.IsDevelopment())
{
spa.UseReactDevelopmentServer(npmScript: "start");
}
});
}
发布应用程序并运行后,应用程序工作正常,除了 Hangfire 仪表板,并尝试访问路由:/Hangfire
导致服务器返回 SPA index.html
但是,如果我刷新页面进行硬重新加载,仪表板加载正常。
Swagger 也会发生同样的事情。
有人可以帮帮我吗?
解决方案
在遵循Guilherme的建议后,取消注册 service worker 解决了这个问题。我没有忽略路线,而是选择取消注册。
非常感谢 !
推荐阅读
- laravel - 试图用碳 laravel 获得日期的差异
- ant - 使用ant读取build.xml,按照写入的顺序
标签 - amazon-web-services - 我可以将 Amazon 证书与我的 EC2 实例(网站)一起使用吗?
- asp.net - 如何在 ASP.NET 中从数据库中保存和检索图像
- python - 找到动态加载的文本 Python Selenium WebDriver
- css - Flexbox - 图像和内容
- android - RecyclerView + NestedScrollView + BottomSheetBehavior = 性能不佳?
- javascript - 使用javascript获取每个表列值
- angular-guards - 在 Angular 4 防护中使用服务
- android - Android,从 Firebase 查询数据后无法初始化对象