In this article, we are discussing the differences between AddMVC () and AddMvc Core (). As a beginner in the .NET language, you must have stumbled upon these two methods in the configure services () of the startup class.
Both the methods look quite similar and their respective help tooltips simply tell us this:
Adds MVC services to the specified IService Collection
Adds essential MVC services to the specified to the specified Iservice Collection
While creating an Asp.net development services with Web API, you usually see a Startup.cs file to configure your services and configure your pipeline. Configure services is preparing for your services for being used as you configure them. .NET Core is a modular rewrite of the .NET cross-platform framework. This new framework comes with so many amazing features that make it popular across regions. One of the best features we can talk about this framework is Dependency Injection. It's like everything is on demand and you have to enable whatever you need.
This is the place to add dependency injection, but that is a different topic. Dependency Injection is a unique design pattern in which a class or object has its dependent classes injected rather than creating them directly. It facilitates loose coupling and promotes testability and maintenance. It also allows you to change your implementation without changing the classes or interfaces that leverage those implementations.
You can have a lot of configuration here but we want to focus on the main topic, that means adding the MVC framework.
AddMvcCore is an extension method on the IService Collection in the MVC framework. Using an extension method is the most common pattern for registering groups of related services into the service collection. Same goes for this extension method and this can be used to register the MVC series that the more commonly when building the MVC application. This extension method is very useful when your application is not taking advantage of all MVC features.
In the visual studio, when we start with "File" - "New Project" the default setting in the method is AddMvC(). And it works straight away. When building the REST APIs, here, there is no need for Razor components, we often start with AddMvcCore, which makes the process more contemporary.
By adding this method, it enables the minimum dependency needed to run the MVC framework. AddMvcCore method adds significant MVC series to the specified method.
Features of this method:
Application Part manager: It manages the parts and features of an MVC application.
Default Feature Provider: This feature adds the controller feature providers.
Default services: This feature is used to add the routing.
Core Services: These are basic services to run the MVC framework with comparatively minimum dependencies.
If you want to use the MVC framework, firstly you have to enable or you have to take the MVC project template. This method adds all dependencies in the specified project. This method first calls the AddMvcCore extension. Due to this reason, all of the same setup and service registration occurs, including the creation of an application manager.
Key features of this method:
API Explorer: Enables the API help pages.
Authorization: Used for the security and authorization of web pages, which is a very important feature.
MVC Core: This is the core dependency of the MVC framework and they are required to run the MVC framework with minimum dependencies.
Default Framework parts: It adds some application part dependencies. The application part manager takes care of application parts and application features.
For matter Mappings: A filter will use the format value in the route data or query string to set the content type on an object result.
Views: By the help of this feature, you can see views feature and makes it work and makes the functionality better.
Cache tag Helper: Cache Tag helpers enables server-side code to participate in creating and rendering HTML elements in Razor files.
Razor View engine: Parser and code generator for CS-HTML files are used in view pages for MVC web apps.
Cors: This feature enables the cross-origin resource sharing (CorS) which is a mechanism that allows to restrict resources and allows them to use in a convenient way.
If we compare both the extension method, the AddMvcCore method, which laid more groundwork by creating the Application Part manager. Generally, we call extensions to register extra services. As you can see in this article, many of those services are more specific to web applications which need to work with views. For API only applications, it is quite possible you won't need most of these. In the API projects, we start with AddMvcCore and simply add in the few items we need to use the builder extension methods and complete the intended tasks.