Before creating the controller, we need to remove the following app.Run statement as this will return Hello World! for all the incoming requests. As we want incoming requests to be handled by the controllers, we need to remove the following code from the Configure method of the Startup class:
app.Run(async (context) =>
{
await context.Response.WriteAsync("Hello World!");
};
We have installed ASP.NET Core in our application. So, we are geared up for creating our first ASP.NET Core controller. Create a folder with the name Controllers and add a new controller from the context menu, as shown in the following screenshot:
Context basically represents the request-response pair along with other metadata necessary to process the request.
Once you navigate to Add | New Item, you will be shown the following list of options. We are going to add an MVC controller class to our project:
A class will be created with the following content:
public class HomeController : Controller
{
// GET: /<controller>/
public IActionResult Index()
{
return View();
}
}
All controllers, both MVC and Web API controllers, inherit from the Controller base class. In earlier versions of ASP.NET MVC, MVC controllers would inherit from the Controller class and Web API controllers would inherit from the APIController class.
In the preceding HomeController class, we have a single action method by Index that returns the corresponding view. When you run the application as it is, you'll get a 500 Internal Server Error. The reason for this is that no view has been created for the Index action of the HomeController and ASP.NET Core tries to search for that view. As the view is not available, it returns a 500 Internal Server Error with the message "InvalidOperationException: The view 'Index' was not found. The following locations were searched:". Whenever a status code starts with 5XX, then we think that it is the servers, fault. Whenever a status code starts with 4XX, then it is client related.
Instead of creating and returning that view, let us make a simple change to this action method. Let us return a string, Hello World! I am learning MVC!, and change the return type of IActionResult:
public string Index()
{
return "Hello World! I am learning MVC!";
}
Run the application. You'll see the Hello World! I am learning MVC! in your browser, as shown in the following screenshot. Please make sure that you remove the app.Run statement in the Configure method, as mentioned earlier:
Voila! We have changed the ASP.NET Core application to render the custom content instead of the boring Hello World! What we have done may seem like a marginal improvement, but we have used controllers and action methods in our ASP.NET Core application, which has brought a lot of structure and flexibility to the web application development.
The following screenshot shows what happens in the background when we run the application:
The following is the sequence of steps that occur when we run the application:
- The application runs on the URL http://localhost:50140, where 50140 is the port number selected by IIS Express to run the application on my local system. This number may vary.
- As we have not passed any parameter, default values for the Controller and action methods will be selected. In our case, HomeController will be chosen as the Controller and Index will be chosen as the action method in the HomeController. Since ID is the optional value and it is not passed, this ID parameter is ignored.
- After the Controller and action methods are selected by the routing engine, control is passed to the action method of the selected controller. In our case, it will be the Index action method of the HomeController.
- In the Index action method, we are returning a string, Hello World! I am learning ASP.Net MVC! This text is returned from the controller, which would then return back to the user.