reverse-proxy icon indicating copy to clipboard operation
reverse-proxy copied to clipboard

YARP Reverse Proxy not working correctly with Blazor SSR Project in .net8

Open htmlsplash opened this issue 1 year ago • 3 comments

Describe the bug

We have 2 projects in our solution:

  1. Legacy webforms (on .net framework 4.8) and
  2. Blazor Server (SSR) project for .net 8.

We have setup YARP for the Blazor project but it doesn't properly work. For resources (bootstrap.min.css, app.css) located in the www folder (of the Blazor app) the YARP proxy forwards them to the legacy webforms app although they exist in the Blazor app.

To Reproduce

Here's the program file:

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorComponents()
	.AddInteractiveServerComponents();


// web adapters/setup YARP
builder.Services.AddSystemWebAdapters();
builder.Services.AddHttpForwarder();



var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
	app.UseExceptionHandler("/Error", createScopeForErrors: true);
	// 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.UseAntiforgery();

app.UseSystemWebAdapters();


// endpoints
app.MapRazorComponents<App>()
	.AddInteractiveServerRenderMode();


// yarp
app.MapForwarder("/{**catch-all}", app.Configuration["ProxyTo"]).Add(static builder => ((RouteEndpointBuilder)builder).Order = int.MaxValue);


app.Run();

appsettings.json (omitted logging info)

"AllowedHosts": "*", "ProxyTo": "http://localhost:60675/"

Further technical details

The webforms runs on .net framework 4.8 The Blazor server app (SSR) runs on .net 8 Testing is done on Windows 10

htmlsplash avatar Feb 06 '24 23:02 htmlsplash

UseStaticFiles will only short-circuit the pipeline if no other route is found. See https://github.com/microsoft/reverse-proxy/discussions/1712#discussioncomment-2738624

To always give priority to static files, you must be explicit as to where you want the routing middleware to live. Try

app.UseStaticFiles();
+app.UseRouting();
app.UseAntiforgery();

MihaZupan avatar Feb 20 '24 09:02 MihaZupan

@MihaZupan That solved the issue, thank you. This is a difficult thing to spot especially since UseRouting middleware is not added by Blazor template. This should be somewhere in the docs. Many people will run into the same problem.

htmlsplash avatar Feb 20 '24 18:02 htmlsplash

This has come up a few times and it's not obvious to diagnose, seems worth adding to an FAQ

MihaZupan avatar Feb 27 '24 09:02 MihaZupan