Back to: ASP.NET Core Tutorials For Beginners and Professionals
AddController vs AddMvc vs AddControllersWithViews vs AddRazorPages
In this article, I am going to discuss the AddController() vs AddMvc() vs AddControllersWithViews() vs AddRazorPages() Methods in ASP.NET Core Web Application. We will also discuss when to use what methods in ASP.NET Core. Before proceeding to this article, I strongly recommend you read our previous article, where we discussed How to set MVC using the AddMvc Method in the ASP.NET Core application.
Different MVC Services Methods Available in ASP.NET Core:
If you go to the definition of the AddMvc() Extension Method, then you will see that 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. And further, each method has two overloaded versions available. One overloaded version does not take any parameter, while the other overloaded version takes the Options object as the parameter using which you can customize the service.
Let us discuss what all these methods are and what features they provide in detail one by one. For a better understanding, please have a look at the following image.
Features of All the Above Methods:
Controller:
Support for the Controller is available for all the Methods. So, you can use any of the four methods if you need only a controller. Controllers are responsible for controlling the flow of the application execution. When you make a request to MVC or Web API application, it is the controller action method that will handle the request and return the response.
Model Binding:
The Model Binding feature is also available for all four methods. Model binding is used to map the incoming HTTP Request data to the controller action methods. HTTP Request data means when a user makes a request with form data from the browser to a Controller, at that time model binder works as a middleman to map the incoming HTTP request data with the Controller action method.
API Explorer:
Except for the AddRazorPages method, all other methods support the API Explorer feature. API Explorer contains functionality for exposing metadata about your applications. We can use it to provide details such as a list of controllers and actions, their URLs, allowed HTTP methods, parameters, response types, etc.
Authorization:
Authorization is available for all four methods. Authorization is basically used to provide security features. Authorization is a process used to determine whether the user has access to a particular resource. In .NET MVC and Web API Applications, we can use Authorize Attribute to implement Authorization.
CORS:
Again, except for the AddRazorPages method, all other methods support CORS. CORS stands for cross-origin resource sharing. CORS is basically a feature that allows CROSS domain call. That means they can access your method from other domains using jQuery AJAX. In simple words, we can say that it is a mechanism to bypass the Same-Origin policy of a Web browser.
Validation:
The validation feature is supported by all the methods. Validation is basically used to validate the HTTP Request data. We can use DataAnnotations attributes and Fluent API to implement Validations. DataAnnotations includes built-in validation attributes for different validation rules, which can be applied to the properties of the model class. At the same point, we need to use Fluent AP for condition-based validation.
Formatter Mapping:
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:
This feature is unavailable in the AddControllers method and is available for the other three methods. To prevent CSRF attacks, ASP.NET Core MVC uses anti-forgery tokens, also called request verification tokens. The client requests an HTML page that contains a form. The server includes two tokens in the response. One token is sent as a cookie. The other is placed in a hidden form field.
TempData:
This feature is unavailable in the AddControllers method and is available for the other three methods. TempData in ASP.NET Core MVC can be used to store temporary data, which can be used in the subsequent request.
Views:
This feature is unavailable in the AddControllers method and is available for the other three methods. The View is a user interface that displays data from the model to the user, enabling the user to modify the data.
Pages:
The Pages are available only with AddMVC and AddRazorPages methods. Razor Pages are designed for page-focused scenarios; each page can handle its own model and actions.
Tag Helpers:
Tag Helpers are not available in the AddControllers method and are available for the rest three methods. Tag Helper is a new feature in ASP.NET Core MVC that enables the server-side code to create and render HTML elements.
Memory Cache:
The Memory Cache feature is also not available in the AddControllers method but is available with the rest three methods. Memory cache data is stored in the memory of the local web server. When the Web application is hosted on a single web server, then memory caching implemented for that web application uses the memory of that host server.
Which Method to Use in Our Application?
This depends on which type of application you want to create.
- If you want to create a Web API Application, i.e., Restful Services, where there are no views, then you need to use AddControllers() extension method.
- If you want to work with the Razor Pages Application, then you need to use the AddRazorPages() extension method in your Main method of the Program class.
- If you want to develop a Model View Controller, i.e., an MVC application, you need to use the AddControllersWithViews() extension method. Further, if you want Pages features in your MVC application, then you need to use the AddMVC method.
AddMvc: This method has all the features. So, you can use this AddMVC method with any type of application (Web API, MVC, and Razor Pages).
Note: Adding AddMvc() method will add extra features even though they are not required to your application which might impact the performance of your application. So, depending on the requirement, you need to choose the appropriate method.
In the next article, I am going to discuss Models in ASP.NET Core MVC Applications with Examples. Here, in this article, I try to explain the AddController vs AddMvc vs AddControllersWithViews vs AddRazorPages Extension Methods in the 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 the ASP.NET Core article.
About the Author:
Pranaya Rout has published more than 3,000 articles in his 11-year career. Pranaya Rout has very good experience with Microsoft Technologies, Including C#, VB, ASP.NET MVC, ASP.NET Web API, EF, EF Core, ADO.NET, LINQ, SQL Server, MYSQL, Oracle, ASP.NET Core, Cloud Computing, Microservices, Design Patterns and still learning new technologies.
Nice Explanation i really loved your tutorials.
This comment is not for publish.
Please instead of
As asp.net core is open source, so you can the source code
write
As asp.net core is open source, so you can see the source code
Hi,
Thanks for identifying the typographical error. We have corrected it.
Thanks for sharing valuable information, very good explanation.
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.
Mistake:
even though which.
Correct
including those that may not be required.
Very concise and excellent clarification on the subject matter