c# - YARP Reverse Proxy in Blazor Server - Static Files (CSSJS) Not Loading (404) - Stack Overflow

I'm implementing YARP (Yet Another Reverse Proxy) in my Blazor Server application to proxy multipl

I'm implementing YARP (Yet Another Reverse Proxy) in my Blazor Server application to proxy multiple projects, but I'm facing some issues.

Issues Observed:

  1. Static Files (CSS/JS) Not Loading - 404 Errors
  • The browser console shows multiple Failed to load resource: the server responded with a status of 404.

  • Example missing files

error message

Blazor Circuit Initialization Failure

  • After the WebSocket connection is established, I get the following error:

[Error] The circuit failed to initialize. Connection disconnected.

Project Setup & Code Snippets

Program.cs Configuration

builder.Services.AddReverseProxy();
builder.Services.AddSingleton<IProxyConfigProvider, DynamicProxyConfigProvider>();

app.UseStaticFiles();
app.MapReverseProxy();
app.MapRazorComponents().AddInteractiveServerRenderMode();

DynamicProxyConfigProvider.cs (Route & Cluster Configuration)

routes.Add(new RouteConfig
{
    RouteId = routeId,
    ClusterId = clusterId,
    Match = new RouteMatch { Path = $"{proxiedUrl}/{{**catch-all}}" },
    Transforms = new List<Dictionary<string, string>>
    {
        new Dictionary<string, string> { { "PathRemovePrefix", proxiedUrl } }
    }
});

clusters.Add(new ClusterConfig
{
    ClusterId = clusterId,
    Destinations = new Dictionary<string, DestinationConfig>
    {
        { "destination", new DestinationConfig { Address = item.Url } }
    }
});

Index Page (Navigation to Proxy URL)

string sanitizedTitle = Uri.EscapeDataString(item.Title.Trim().Replace(" ", "-").ToLower()); 

string proxiedUrl = $"/system/{sanitizedTitle}-{item.Id}"; 

Navigation.NavigateTo(proxiedUrl, true); 

Steps to Reproduce:

Set up a Blazor Server app with YARP to proxy an external Blazor app. Open the browser console (F12) to see 404 errors for static files and note a circuit failure when initializing a Blazor session.

What I Have Tried: Ensured Static Files Exist in wwwroot. Added UseStaticFiles() before UseRouting(). Checked _content/ Paths for Blazor resources. Enabled WebSockets in YARP. Modified appsettings.json to match YARP paths.

Expected Behavior: Static files should load correctly.

Environment:

.NET Version: net9.0 YARP Version: 2.3.0 Blazor Server Framework Version: net9.0 Hosting Environment: Localhost (https://localhost:7287) Browser: Edge

Questions:

How do I ensure YARP properly serves static files?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745101291a4611288.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信