AddController vs AddMvc vs AddControllersWithViews vs AddRazorPages
In this article, I will 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 discussing How to set MVC using the AddMvc Method in the ASP.NET Core application.
Different MVC Services Methods Available in ASP.NET Core:
In ASP.NET Core, various methods are available to configure services in the Program.cs file, each catering to different aspects of web application development. Understanding the differences between AddController, AddMvc, AddControllersWithViews, and AddRazorPages can help in choosing the right approach for your specific needs.
Go to the definition of the AddMvc() Extension Method. You will see that along with the AddMvc() method, AddController(), AddControllersWithViews(), and AddRazorPages() methods are also 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 an 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 maps the incoming HTTP Request data (Route Data, Query String Data, Request Body Data, Posted form Data, etc.) to the controller action method parameters. The model binder is a middleman to map the incoming HTTP request data with the Controller action method parameter.
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 ASP.NET Core 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 CROS domain calls. 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 four 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. Simultaneously, 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 (Cross-Site Request Forgery) attacks, ASP.NET Core MVC uses anti-forgery tokens, also called request verification tokens.
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 other 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 other three methods. Memory Cache is a powerful and easy-to-use feature for caching data within the web server’s memory where your application is running. This in-memory cache can significantly improve the performance and scalability of your application by temporarily storing frequently accessed data, such as data from a database or web service.
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 must use the 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 Web Application using the Model View Controller Design Pattern, you need to use the AddControllersWithViews() extension method. Further, if you want Razor Pages features in your MVC application, you must use the AddMVC method.
- Use AddControllers for API-only applications.
- Use AddMvc for applications that need full MVC capabilities (APIs, views, and pages).
- Use AddControllersWithViews for applications that need both APIs and views but not Razor Pages.
- Use AddRazorPages for applications focused on Razor Pages without the need for MVC-style controllers and views.
Note: The AddMvc method has all the features. So, you can use this AddMVC method with any application (Web API, MVC, and Razor Pages). Adding the AddMvc() method will add extra features even though they are not required for your application, which might impact the performance of your application. So, depending on the requirement, you must choose the appropriate method.
In the next article, I will discuss Controllers 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, questions, or comments about this AddController vs AddMvc vs AddControllersWithViews vs AddRazorPages in the ASP.NET Core article.
About the Author: Pranaya Rout
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.