Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
htmlsplash opened this issue Feb 6, 2024 · 3 comments
Labels
Type: Documentation Improvements or additions to documentation
Milestone

Comments

@htmlsplash
Copy link

htmlsplash commented Feb 6, 2024

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 htmlsplash added the Type: Bug Something isn't working label Feb 6, 2024
@htmlsplash htmlsplash changed the title YARP Reverse Proxy not working correctly with Blazor SSR template in .net8 YARP Reverse Proxy not working correctly with Blazor SSR Project in .net8 Feb 6, 2024
@MihaZupan
Copy link
Member

UseStaticFiles will only short-circuit the pipeline if no other route is found.
See #1712 (comment)

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();

@htmlsplash
Copy link
Author

htmlsplash commented Feb 20, 2024

@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.

@MihaZupan
Copy link
Member

MihaZupan commented Feb 27, 2024

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

@MihaZupan MihaZupan added Type: Documentation Improvements or additions to documentation and removed Type: Bug Something isn't working labels Feb 27, 2024
@MihaZupan MihaZupan added this to the Backlog milestone Feb 27, 2024
@MihaZupan MihaZupan removed their assignment Feb 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants