Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 679
InteXX Avatar asked Nov 07 '25 19:11

InteXX


1 Answers

We use this to configure a classic API in our project:

app.UseEndpoints(endpoints => {
    endpoints
        .MapControllers()
        .RequireAuthorization();
});

We do not setup the port to be used, but I think the option could be available. I just found an interesting article that could help you: https://andrewlock.net/how-to-automatically-choose-a-free-port-in-asp-net-core/

// Dylan

like image 58
Dylan El Bar Avatar answered Nov 09 '25 08:11

Dylan El Bar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!