Search code examples
asp.net-corerouteshangfire

Hangfire Dashboard links point to wrong routes


The problem is, when the app is deployed to a non root context (i.e. http://root/someAppName/ the dashboard is by default accessible at http://root/someAppName/hangfire). But the dashboard lacks any js and css as they are being downloaded from the wrong locations. Same problem (with wrong location) happens with links. They all point to http://root/someLinkName skipping the /someAppName part of the context. I am using ASP.NET Core 2.2.

There are similar questions found here but it is unresolved and, in addition, doesn't refer to ASP.NET Core. How do I properly set-up my app to mitigate this problem? The app cannot be deployed to the root context and needs to stay under /someAppName.


Solution

  • This is a known issue when an ASP.NET Core application with the Hangfire gets deployed behind a gateway or a load balancer. See this, this and this for the reference. The workaround is to configure a gateway (load balancer) to pass X-Forwarded-PathBase to the application and add the following middleware before app.UseHangfireDashboard():

    app.Use((context, next) =>
    {
        var pathBase = context.Request.Headers["X-Forwarded-PathBase"];
        if (pathBase != null)
            context.Request.PathBase = new PathString(pathBase);
        return next();
    });
    
    app.UseHangfireDashboard();