问题
After installing the React app template from .NET Core 3.1 using dotnet new react
the application works perfectly in Development and Production. The problem appears when trying to use Swagger or Hangfire dashboard endpoints.
After the app is created I add the package reference for Hangfire and for practical purposes the memory storage:
<PackageReference Include="Hangfire" Version="1.7.*" />
<PackageReference Include="Hangfire.MemoryStorage" Version="1.7.0" />
In 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");
}
});
}
After publishing the application and run it, the app works fine except the Hangfire dashboard, and trying to access the route: /Hangfire
causes the server return the SPA index.html
But if I refresh the page doing a hard reload, the dashboard loads fine.
Same thing occurs with Swagger.
Can someone give me a hand ?
回答1:
After following Guilherme suggestion, unregistering the service worker solved the issue. Instead of ignoring the routes, I opted to unregister it.
Thank you very much !
来源:https://stackoverflow.com/questions/64381334/net-core-3-1-spa-react-application-doesnt-serve-hangfire-dashboard-swagger-o