AddController vs AddMvc vs AddControllersWithViews vs AddRazorPages
In this article, I am going to discuss the AddController() vs AddMvc() vs AddControllersWithViews() vs AddRazorPages() method in ASP.NET Core application. We will also discuss when to use what methods in ASP.NET Core. I strongly recommended you read our previous article before proceeding to this article where we discussed how to set MVC using the AddMvc method in ASP.NET Core application.
Different methods available in ASP.NET Core:
If you go to the definition of AddMvc() extension method, you will see along with AddMvc() method, there are also other methods (AddController(), AddControllersWithViews(), and AddRazorPages()) available as shown in the below image. All these methods are implemented as an extension method on the IServiceCollection interface.
Let us discuss what all these methods are and what features they provide in detail one by one.
Points to Remember:
- Support of controller is available for all the methods. So, if you need only a controller then you can use any of the methods.
- The model binding feature is also available for all the methods. Model binding is used to map the incoming data to the controller action methods.
- Except for the AddRazorPages method, all other methods support the API Explorer feature. The API Explorer has used the list of all the available APIs in your application.
- Authorization is available for all four methods. Authorization is basically used to provide the security features,
- Again, except for the AddRazorPages method, all other methods support CORS. CORS is basically a feature that allows CROSS domain call. That means from other domains they can access your method using jQuery AJAX.
- The validation feature is supported by all the methods. Validation is basically used to validate the HTTP Request data. In .NET Core Application, we can implement validation using a concept called Data Annotation.
- Except for the AddRazorPages method, all other methods support the Formatter Mapping feature. The Formatter Mapping feature is basically used to format the output of your action method such as JSON or XML, etc.
- Antiforgery, TempData, and Views features are not available in the AddControllers method.
- The Pages are available only with AddMVC and AddRazorPages method.
- TagHelpers are not available in the AddControllers method and available for rest three methods.
- The memory Cache feature is also not available in the AddControllers method but available with the rest three methods.
Which method to use for our application?
This is depending on which type of application you want to create.
- If you want to create a Web API application where there are no views, then you need to use AddControllers() extension method.
- If you want to work with the Razor Page application, then you need to use the AddRazorPages() extension method into your ConfigureService method of Startup class.
- 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.
AddMvc: This method has all the features. So, you can any type of application (Web API, MVC, and Razor Pages) using this AddMVC method.
Note: Adding AddMvc() method will add extra features even though which are not required to your application which might impact the performance of the application.
In the next article, I am going to discuss working with Models in the ASP.NET Core MVC application. Here, in this article, I try to explain the AddController vs AddMvc vs AddControllersWithViews vs AddRazorPages in ASP.NET Core application. I would like to have your feedback. Please post your feedback, question, or comments about this AddController vs AddMvc vs AddControllersWithViews vs AddRazorPages in ASP.NET Core article.