Search code examples
c#asp.net-corerazor-pages.net-7.0asp.net-core-7.0

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


Solution

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