AddController vs AddMvc vs AddControllersWithViews vs AddRazorPages

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.

AddController vs AddMvc vs AddControllersWithViews vs AddRazorPages

Let us discuss what all these methods are and what features they provide in detail one by one.

AddController() vs AddMvc() vs AddControllersWithViews() vs AddRazorPages() method in ASP.NET Core application

Points to Remember:
  1. Support of controller is available for all the methods. So, if you need only a controller then you can use any of the methods.
  2. 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.
  3. 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.
  4. Authorization is available for all four methods. Authorization is basically used to provide the security features,
  5. 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.
  6. 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.
  7. 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.
  8. Antiforgery, TempData, and Views features are not available in the AddControllers method.
  9. The Pages are available only with AddMVC and AddRazorPages method.
  10. TagHelpers are not available in the AddControllers method and available for rest three methods.
  11. 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.

  1. If you want to create a Web API application where there are no views, then you need to use AddControllers() extension method.
  2. 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.
  3. 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.

4 thoughts on “AddController vs AddMvc vs AddControllersWithViews vs AddRazorPages”

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

Leave a Reply

Your email address will not be published. Required fields are marked *