Loading

ASP.NET Core

How to AddController vs AddMvc vs AddControllersWithViews vs AddRazorPages. The Complete ASP.NET Core Developer Course 2023 [Videos].

AddMvcCore() , as the name implies, only adds core components of the MVC pipeline, requiring you to add any other middleware (needed for your project) by yourself. AddMvc() internally calls AddMvcCore() and adds other middleware such as the Razor view engine, Razor pages, CORS, etc

AddController vs AddMvc vs AddControllersWithViews vs AddRazorPages

In this Video, 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 Video before proceeding to this Video where we discussed  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.

AddController vs AddMvc vs AddControllersWithViews vs AddRazorPages

  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 Video, I am going to discuss working with application. Here, in this Video, 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 Video.

See All

Comments (235 Comments)

Submit Your Comment

See All Posts

Related Posts

ASP.NET Core / Blog

What is ASP.NET Core?

ASP.NET Core is the new version of the ASP.NET web framework mainly targeted to run on .NET Core platform.
27-jan-2022 /16 /235

ASP.NET Core / Blog

What is ASP.NET Core Framework?

ASP.NET Core is a cross-platform, high-performance, open-source framework for building modern, cloud-enabled, Internet-connected apps. With ASP.NET Core, you can: Build web apps and services, Internet of Things (IoT) apps, and mobile backends. Use your favorite development tools on Windows, macOS, and Linux.
27-jan-2022 /16 /235

ASP.NET Core / Blog

How to Setup ASP.NET Core Environment ?

ASP.NET Core is a cross-platform, high-performance, open-source framework for building modern, cloud-enabled, Internet-connected apps. With ASP.NET Core, you can: Build web apps and services, Internet of Things (IoT) apps, and mobile backends. Use your favorite development tools on Windows, macOS, and Linux.
27-jan-2022 /16 /235