首页 > 解决方案 > .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 也会发生同样的事情。

有人可以帮帮我吗?

标签: reactjs.net-coresingle-page-applicationhangfire

解决方案


在遵循Guilherme的建议后,取消注册 service worker 解决了这个问题。我没有忽略路线,而是选择取消注册。

非常感谢 !


推荐阅读