Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need services.AddMvc or not?

Tags:

asp.net-core

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?

like image 962
Cherven Avatar asked Jan 03 '20 03:01

Cherven


People also ask

What is service AddMVC?

Adds MVC services to the specified IServiceCollection. C# Copy. public static Microsoft.Extensions.DependencyInjection.

What is the difference between AddMVC and AddControllersWithViews?

If you want to develop a Model View Controller i.e. MVC application then you need to use AddControllersWithViews() method. Further, if you want Pages features into your MVC application, then you need to use the AddMVC method.

What is service AddRazorPages?

AddRazorPages(IServiceCollection) Adds services for pages to the specified IServiceCollection. AddRazorPages(IServiceCollection, Action<RazorPagesOptions>) Adds services for pages to the specified IServiceCollection.

What is Addcontrollers?

The AddController method Registers everything that is needed for Web API Development. The services include Support for Controllers, Model Binding, API Explorer, Authorization, CORS, Validations, Formatter Mapping, etc.


2 Answers

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

like image 54
Ryan Avatar answered Oct 17 '22 21:10

Ryan


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

like image 1
Manish Avatar answered Oct 17 '22 23:10

Manish