Search code examples
asp.net-coreroutesdefault

How to Default Route in ASP.NET Core 6 MVC?


I'm trying to learn ASP.NET Core 6, and now following a tutorial using ASP.NET Core 2. After adding the controller phase they delete this line from the startup file

app.run(async (context) = await context.response.writeasync("hello world"))

and add

app.UseMvcWithDefaultRoute(); 

so they can see the views.

As you know there is no startup file in ASP.NET Core 6, and I don't know what to do at this point.

I searched the internet a lot and have nothing.

I try to adjust the code in the program file like that

app.MapGet("/", () => app.UseMvcWithDefaultRoute);

but that does not work.


Solution

  • In asp.net core6 we define the default route like this:

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

    You can read Routing to controller actions in ASP.NET Core to know more.