In this article we are going to discuss differences between AddMVC() vs AddControllersWithViews() vs AddControllers() vs AddRazorPages() in ASP .NET Core 3.x. Before that please go through my previous article Difference between AddMVC() and AddMvcCore().
Difference between AddMVC() vs AddControllersWithViews() vs AddControllers() vs AddRazorPages()
In .NET Core 2.x and earlier, we could register the MVC framework in the ASP.NET Core dependency injection container in two ways:
- services.AddMvc()
- services.AddMvcCore()
In ASP.NET Core 3.x, aside from those two approaches, there are three additional ways:
- services.AddControllers()
- services.AddControllersWithViews()
- services.AddRazorPages()
AddMvc() | AddControllerWithViews() | AddController() | AddRazorPages() |
---|---|---|---|
Controllers | Controllers | Controllers | Controllers |
Model Binding | Model Binding | Model Binding | Model Binding |
API Explorer | API Explorer | API Explorer | — |
Authorization | Authorization | Authorization | Authorization |
CORS | CORS | CORS | — |
Validations | Validations | Validations | Validations |
Formatter Mapping | Formatter Mapping | Formatter Mapping | — |
Antiforgery | Antiforgery | — | Antiforgery |
Temp Data | Temp Data | — | Temp Data |
Views | Views | — | Views |
Pages | — | — | Pages |
Tag Helpers | Tag Helpers | — | Tag Helpers |
Memory Cache | Memory Cache | — | Memory Cache |
Controllers : Controllers are responsible for controlling the flow of the application execution. When you make a request (means request a page) to MVC application, a controller is responsible for returning the response to that request.
Model Binding: Model Binding allows to map HTTP Request data with the model. 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 with Controller action method.
API Explorer: API Explorer contains functionality for exposing metadata about MVC application. We can use it to provide details such as a list of controllers and actions, their URLs and allowed HTTP methods, parameters and response types etc..
Authorization: Authorization is a process which is used to determine whether the user has access to a particular resource or not. In ASP .NET MVC it controlled through the AuthorizeAttribute attribute.
CORS: CORS stands for cross-origin resource sharing. It is a mechanism to bypass the Same-Origin policy of a Web browser.
Validations: ASP.NET MVC uses DataAnnotations attributes to implement validations. DataAnnotations includes built-in validation attributes for different validation rules, which can be applied to the properties of model class.
Formatter Mapping: Formatter mapping is used to format the output to the action method to any format like Json, XML etc.
Antiforgery: To prevent CSRF attacks, ASP.NET 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.
Temp Data: TempData in ASP.NET MVC can be used to store temporary data which can be used in the subsequent request.
Views : View is a user interface, View displays data from the model to the user and also enables them to modify the data.
Pages: Razor Pages are designed for page-focused scenarios; each page can handle its own model and actions.
Tag Helpers: Tag Helper is a new feature in ASP.NET MVC Core that enables the server-side code to create and render HTML elements.
Memory Cache: 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 memory of that host server.
AddMvcCore()
registers all the required core services to the MVC application. It adds the minimum essential MVC services to the specified IServiceCollection, additional services including MVC’s support for authorization, formatters, and validation must be added separately using the IMvcCoreBuilder returned from this method. In .NET Core 2.x and earlier, not even JSON support was there; this has now changed and the System.Text.Json formatter is actually already included in the call to AddMvcCore().
services.AddMvcCore() .AddDataAnnotations() // for model validation .AddApiExplorer(); // for Swagger
AddControllers()
was introduced in ASP.NET Core 3.0 as a mechanism that calling the lightweight AddMvcCore().
- Everything that AddMvcCore() does.
- Authorization services – needed for authorization policies, filters and other authorization components to work
- API explorer – required if you want to build dynamic API documentation, generate Swagger/OpenAPI files
- Data annotations – Needed for model validation.
- Formatter mappings – needed for content negotiation to work
- CORS
This should be the default choice for you if are developing an API and want to quickly and reliably bootstrap the framework.
// ready for API development services.AddControllers();
AddControllersWithViews()
– Using this we can develop as classic MVC site with controllers and Razor views.
- Everything that AddControllers() does.
- Views functionality – explicitly registers the Razor view engine.
- Cache tag helper.
// ready for "classic" MVC website development // and at the same time ready for API development services.AddControllersWithViews();
AddRazorPages()
is intended to serve as a bootstrapping helper for working with the new Razor Pages feature. Under the hood, it ends up activating the following:
- All the core Razor pages features
- Everything that AddMvcCore() does – this is a bit surprising at first glance
- Authorization services – needed for authorization policies, filters and other authorization components to work
- Data annotations – Data annotation is use for model validation.
- Cache tag helper
// ready for Razor Pages development // and at the same time quite ready for API development services.AddRazorPages();
AddMvc()
which simply registers the entire MVC features added in ASP .NET Core application.
- Everything that AddControllersWithViews() does
- Everything that AddRazorPages() does
// ready for everything services.AddMvc()
Conclusion
Leave behind your valuable queries and suggestions in the comment section below. Also, if you think this article helps you, do not forget to share this with your developer community. Happy Coding 🙂
Jayant Tripathy
Coder, Blogger, YouTuberA passionate developer keep focus on learning and working on new technology.