In this article, I am going to discuss the Controllers in ASP.NET Core MVC application with an example. Please read our previous article before proceeding to this article where we discussed ASP.NET Core Dependency Injection with an example. As part of this article, we are going to discuss the following pointers.
The Controllers in ASP.NET Core MVC application are the classes having a set of public methods. These public methods are called as actions (action methods). These action methods are the methods of a Controller which is actually going to handle the incoming HTTP Requests.
The Controllers in MVC application logically group similar types of actions together. This aggregation of actions or grouping similar types of action together allows us to define sets of rules such as caching, routing, and authorization which is going to be applied collectively.
By convention, the controller classes in ASP.NET Core MVC application should reside in the project’s root-level Controllers folder and inherits from the Microsoft.AspNetCore.Mvc.Controller base class.
We can consider a class as a Controller when at least one of the following conditions is true.
With the above Controller place in our application, whenever the user type the following URL and hit the enter key
http://localhost:xxxx/home/GetStudentDetails/102
Then the above URL “/home/GetStudentDetails/102” is mapped to the “GetStudentDetails” method of the HomeController as shown in the below image.
How the above mapping is done that we will discuss in our upcoming articles. For now, just understand that the above mapping is done by the routing rules which are defined for your application.
In ASP.NET Core MVC the action methods of a controller can return different types of data such as JSON, View, String, Object, XML, etc.
Controller Action Method Returning JSON Data:
In the below example, the GetStudentDetails action method always going to returns the data in JSON format irrespective of the content negotiation. This is because of the return type of the GetStudentDetails() method which is set to JsonResult. In this case, it is going to ignore the Accept Header values.
Controller Action Method returning ObjectResult:
In the following example, it looks for the Accept Header value and if the value is set to application/xml, then it returns the data in XML format whereas if the value is set to application/json, then the data is going to return in JSON format.
Note: In order to return XML data from an action method, you need to register the XML Serializer Formatter by calling the AddXmlSerializerFormatters() method within the ConfigureServices() method of the Startup.cs class as shown in the below image.
Controller Action Method returning View:
In order to return a view from an action method in ASP.NET Core MVC, you need to use ViewResult as the return type of the action method. In the following example, the GetStudentDetails action method is going to return a View as the return of the method is set to ViewResult.
At this point, if you run the application and navigate to “/home/GetStudentDetails/102” URL, then you will get the following error.
The reason for the above error is we do not have the required View file in our application. In the next article, I am going to discuss the Views in the ASP.NET Core MVC application. In this article, I try to explain Controllers in the ASP.NET Core MVC application. I hope you enjoy this article.
Summary:
I hope this post will be helpful to understand the concept of Controllers in ASP.NET Core MVC
Please share this post with your friends and colleagues.
For any queries please post a comment below.
Happy Coding 😉
- What are Controllers in ASP.NET Core MVC Application?
- What are action Methods?
- How to return JSON Data from Controller Action Method?
- Returning ObjectResult from Controller Action Method in ASP.NET Core MVC Application?
- How to return a View from the Controller Action Method?
The Controllers in ASP.NET Core MVC application are the classes having a set of public methods. These public methods are called as actions (action methods). These action methods are the methods of a Controller which is actually going to handle the incoming HTTP Requests.
The Controllers in MVC application logically group similar types of actions together. This aggregation of actions or grouping similar types of action together allows us to define sets of rules such as caching, routing, and authorization which is going to be applied collectively.
By convention, the controller classes in ASP.NET Core MVC application should reside in the project’s root-level Controllers folder and inherits from the Microsoft.AspNetCore.Mvc.Controller base class.
We can consider a class as a Controller when at least one of the following conditions is true.
- The class name is suffixed with the word “Controller” or if the class inherits from a class whose name is suffixed with the word “Controller”
- The class is decorated with the [Controller] attribute.
With the above Controller place in our application, whenever the user type the following URL and hit the enter key
http://localhost:xxxx/home/GetStudentDetails/102
Then the above URL “/home/GetStudentDetails/102” is mapped to the “GetStudentDetails” method of the HomeController as shown in the below image.
How the above mapping is done that we will discuss in our upcoming articles. For now, just understand that the above mapping is done by the routing rules which are defined for your application.
In ASP.NET Core MVC the action methods of a controller can return different types of data such as JSON, View, String, Object, XML, etc.
Controller Action Method Returning JSON Data:
In the below example, the GetStudentDetails action method always going to returns the data in JSON format irrespective of the content negotiation. This is because of the return type of the GetStudentDetails() method which is set to JsonResult. In this case, it is going to ignore the Accept Header values.
Controller Action Method returning ObjectResult:
In the following example, it looks for the Accept Header value and if the value is set to application/xml, then it returns the data in XML format whereas if the value is set to application/json, then the data is going to return in JSON format.
Note: In order to return XML data from an action method, you need to register the XML Serializer Formatter by calling the AddXmlSerializerFormatters() method within the ConfigureServices() method of the Startup.cs class as shown in the below image.
Controller Action Method returning View:
In order to return a view from an action method in ASP.NET Core MVC, you need to use ViewResult as the return type of the action method. In the following example, the GetStudentDetails action method is going to return a View as the return of the method is set to ViewResult.
At this point, if you run the application and navigate to “/home/GetStudentDetails/102” URL, then you will get the following error.
The reason for the above error is we do not have the required View file in our application. In the next article, I am going to discuss the Views in the ASP.NET Core MVC application. In this article, I try to explain Controllers in the ASP.NET Core MVC application. I hope you enjoy this article.
Summary:
I hope this post will be helpful to understand the concept of Controllers in ASP.NET Core MVC
Please share this post with your friends and colleagues.
For any queries please post a comment below.
Happy Coding 😉
0 comments:
Post a Comment
If you like this website, please share with your friends on Facebook, Twitter, LinkedIn.