最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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?

发布评论

评论列表(0)

  1. 暂无评论