Introduction
In my previous 2 articles, we saw How to get the version of our MVC application and How to create our own MVC application. Now this article explains controllers, controller actions, and action results. After reading this article you will understand how visitors visit an ASP.NET MVC website using a controller.
Controllers
In my previous article (How to create our own MVC application), I explained that a controller is just a .cs file (C# file) or a controller containing actions (methods) and other C# code. Now in this article, I am explaining that when a user request for an MVC page is processed then the request goes to the controller and the MVC Controller is responsible for responding to that specific request.
To understand this let's use the example of a Controller named "Employee" and in that, I have an action "EmployeeNames" as follows:
Here EmployeeController is a controller name and in this is an action (method) that is EmployeeNames. Now when we run this project we will get an error like as follows.
In this error, we can see that "The resource cannot be found". In other words that server is searching for something that is not found.
Note. Actually, in an MVC application, we request for the specific controller or specific controller and the controller's action.
In the preceding example, we have a controller named EmployeeController and the action is EmployeeName. So we will request the controller using the URL. If we see our URL then it is "http://localhost:23491/". Now we will request the controller like as follows.
In this case, EmployeeController's Action EmployeeNames is invoked. The EmployeeNames action is responsible or the EmployeeController is responsible for generating the response to the browser request.
Controller actions
A controller exposes controller actions. An action is a method on a controller that is called when you enter a specific URL in your browser address.
In the preceding example, we are calling an action named EmployeesName using the following URL.
http://localhost:23491/Employee/EmployeeNames
Note
- A controller action must be a public method and as we know C# methods are private by default. Any public method that you add to a controller class is exposed as a controller action automatically.
- A method used as a controller action cannot be overloaded.
- A method used as a controller action cannot be static.
Now in the previous example (How to create our own MVC application), we didn't do the request like this. We used the URL "http://localhost:23491/" to run the MVC application and we didn't get an error. This is because the default controller is HomeController and the default action is the index method that is configured in the RouteConfig.cs file present in the App_Start folder. So open the RouteConfig.cs file and change the default controller to Employee and the default action to EmployeeNames.
RouteConfig.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace DemoMVC
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Employee", action = "EmployeeNames", id = UrlParameter.Optional }
);
}
}
}
Now again run your application you will not get an error.