Cherven
Cherven

Reputation: 1131

Do I need services.AddMvc or not?

I'm trying to find out why in all docs I see services.AddMvc or services.AddMvcCore in Startup.cs but in ASP.NET Core MVC 3.1 template that's created by VS I do not have AddMvc but mvc still works....

the only thing I have related t MVC is

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

can anyone explain why?

Upvotes: 10

Views: 14598

Answers (2)

Manish
Manish

Reputation: 1182

I think all the docs you are seeing is for .net core 2.*.
For More Information -> https://learn.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-2.2&tabs=visual-studio

Upvotes: 0

Ryan
Ryan

Reputation: 20116

As the doc has said that asp.net core 3.0+ template use these new methodsAddControllersWithViews,AddRazorPages,AddControllers instead of AddMvc.

However, AddMvc continues to behave as it has in previous releases.AddMvc() is really just a wrapper around a bunch of other methods that register services. See Source:

https://github.com/aspnet/AspNetCore/blob/0303c9e90b5b48b309a78c2ec9911db1812e6bf3/src/Mvc/Mvc/src/MvcServiceCollectionExtensions.cs#L27

You could either use AddMvc to register for MVC, Razor Pages,API or use individual AddControllersWithViews(for MVC only) and AddRazorPages (for Razor Pages only).

Upvotes: 12

Related Questions