Controllers in ASP.NET Core MVC

Controllers in ASP.NET Core MVC Application

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.

  1. What are Controllers in ASP.NET Core MVC Application?
  2. What are action Methods?
  3. How to return JSON Data from Controller Action Method?
  4. Returning ObjectResult from Controller Action Method in ASP.NET Core MVC Application?
  5. How to return a View from the Controller Action Method?
What are Controllers in ASP.NET Core MVC Application?

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 type of actions together. This aggregation of actions or grouping similar type of action together allows us to define sets of rules such as caching, routing, and authorization which is going to be applied collectively.

Controllers in ASP.NET Core MVC Application

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 of the following conditions is true.

  1. 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”
  2. The class is decorated with the [Controller] attribute.
Let us have a look at the following Controller:

Controllers in ASP.NET Core MVC Application

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.

Mapping URL TO Actions in ASP.NET MVC Core Application

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 JSON Data

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. 

Controller Action Method returning ObjectResult 

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. 

Adding XML Serializer in MVC Core

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. 

Controller Action Method returning View

At this point, if you run the application and navigate to “/home/GetStudentDetails/102” URL, then you will get the following error.

Controller Action Method returning View 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 ASP.NET Core MVC application. In this article, I try to explain Controllers in ASP.NET Core MVC application. I hope you enjoy this article.

Leave a Reply

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