No more UseMvc() in ASP.NET 6, where to put a Use() call?

Viewed 539

I would like requests on different ports (80 and 443 in my case) to be routed to different controllers.

I see a suggested technique in this answer, but the code is outdated under .NET 6. UseMvc() is no longer used in the code provided by the Blazor/ASP.NET project templates.

Here's the boilerplate Program.cs code from a new Blazor WASM project:

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllersWithViews();
builder.Services.AddRazorPages();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
  app.UseWebAssemblyDebugging();
}
else
{
  app.UseExceptionHandler("/Error");
  // 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.UseBlazorFrameworkFiles();
app.UseStaticFiles();

app.UseRouting();

app.MapRazorPages();
app.MapControllers();
app.MapFallbackToFile("index.html");

app.Run();

Given the lack of a UseMvc() call, as indicated in the linked answer, where in this code would I put the suggested app.Use() call?

2 Answers

I ended up putting the Use call first in line, before anything else:

app = builder.Build();
app.Use(async (context, next) => { ... });

Here's the full solution.

Related