15

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!

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
SkyeBoniwell
  • 6,345
  • 12
  • 81
  • 185

1 Answers1

23

AFAIK it should work as is but the warning suggests to register the routes at the top-level of a minimal API 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.

Guru Stron
  • 102,774
  • 10
  • 95
  • 132