ASP.NET Core

MVC Services Methods Available in ASP.NET Core

MVC Services Methods Available in ASP.NET Core

MVC Services Methods Available in ASP.NET Core

Different MVC Services Methods Available in ASP.NET Core:
Asp.Net Core MVC,  referenced several Exension methods as
AddMvc
AddController()
AddControllersWithViews
AddRazorPages


Features of All the Above Methods:


Controller:
Support for the Controller is available for all the Methods. So, you can use any of the 4 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 available for all 4 methods.
Model binding is used to map the incoming HTTP Request data to the controller action methods.
HTTP Request data i.e. 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 4 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:
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 4 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 available in all methods, except AddControllers.
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 available in all methods, except AddControllers.
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 available in all methods, except AddControllers 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 available in all methods excelt AddControllers Method.
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 available in all methods except AddControllers.
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.
For Web API Application / Restful Services: there are no views, then you need to use AddControllers() extension method.
For Razor Pages Application: you need to use the AddRazorPages() extension method in your Main method of the Program class.
For MVC application / Model View Controller: 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 method has all the features. So, you can use this AddMVC method with any type of application (Web API, MVC, and Razor Pages).





Related Post

About Us

Community of IT Professionals

A Complete IT knowledgebase for any kind of Software Language, Development, Programming, Coding, Designing, Networking, Hardware and Digital Marketing.

Instagram