ASP.Net MVC Controller

Introduction

In my previous 2 articles we saw How to get 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 contains actions (methods) and other C# code. Now in this article I am explaining that when a user request for a 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" like 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 a 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 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:
  1. A controller action must be a public method and as we know that C# methods are private by default. Any public method that you add to a controller class is exposed as a controller action automatically.
  2. A method used as a controller action cannot be overloaded.
  3. 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
  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.Linq;   
  4. using System.Web;   
  5. using System.Web.Mvc;   
  6. using System.Web.Routing;   
  7.   
  8. namespace DemoMVC   
  9. {   
  10.    public class RouteConfig   
  11.    {   
  12.       public static void RegisterRoutes(RouteCollection routes)   
  13.       {   
  14.          routes.IgnoreRoute("{resource}.axd/{*pathInfo}");   
  15.   
  16.          routes.MapRoute(   
  17.          name: "Default",   
  18.          url: "{controller}/{action}/{id}",   
  19.          defaults: new { controller = "Employee", action = "EmployeeNames", id = UrlParameter.Optional }   
  20.       );   
  21.       }   
  22.    }   
  23. }   
Now again run your application; you will not get an error.


Similar Articles