Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET 7 and UseEndPoints()

I am trying to convert a .NET Core 3.1 project to .NET 7.

When I use this in my Program.cs class:

app.UseEndpoints(endpoints =>
{
    endpoints.MapRazorPages();

    endpoints.MapControllerRoute(
        name: "default",
        pattern: "{controller=Home}/{action=Index}/{id?}");
});

It gives me this message:

Suggest using top level route registrations UseEndpoints

Then, I clicked on Show potential fixes in Visual Studio and it suggests this:

app.UseEndpoints(endpoints =>
{
    _ = endpoints.MapRazorPages();

    endpoints.MapControllerRoute(
        name: "default",
        pattern: "{controller=Home}/{action=Index}/{id?}");
});

Which looks the same to me.

In .NET 7, what should I do if I need to use RazorPages()?

Thanks!

like image 871
SkyeBoniwell Avatar asked Sep 02 '25 04:09

SkyeBoniwell


1 Answers

AFAIK it should work as is but the warning suggests to register the routes at the top-level of a minimal hosting application, i.e.:

app.MapRazorPages();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

See the ASP0014: Suggest using top level route registrations code analysis rule.

like image 132
Guru Stron Avatar answered Sep 04 '25 18:09

Guru Stron