The ASP.NET MVC Action Methods are responsible for executing and generating requests for answers. It generates a response by default in the form of ActionResult. The actions usually have a one-to-one user interaction mapping.

For instance, enter a URL in your browser, click on a link and submit a form, etc. Every such user interaction sends a request to the server. In each case, the request URL contains information which is used by the MVC framework to invoke an action method. The only restriction on the method of action is that it must be an instance method, so that it can not be static. There are also no restrictions on return value. You can therefore return the string, integer, etc.

Processing Request

Actions are the ultimate request target in an MVC application and the controller base class is used. Let’s look at the processing of requests.

  • When a URL arrives, like /Home/index, the UrlRoutingModule inspects and understands that something configured in the routing table can handle the URL.
  • The UrlRoutingModule compiles the information we have set up in the routing table and gives the MVC route manager control.
  • The MVC route manager passes the controller to the HTTP manager MvcHandler.
  • MvcHandler uses a controller factory to instant the controller and knows which controller to instant because it looks for the controller value in the RouteData.
  • Once the MvcHandler has a controller, the only thing MvcHandler knows about is the IController interface, so the controller simply needs to run.
  • When the controller is told to run, this is derived from the base class of the MVC controller. The execute method creates a caller for action and tells the caller to go and find a method to invoke, to find an action to invoke.
  • Again, the action invoker looks into the RouteData and finds the action parameter that was passed from the routing engine.

Action Types

In essence, actions return different types of results. The ActionResult class is the basis for all results of action. Below is the list of action results and their behavior.

Sr.No.Name
1 ContentResult It returns string
2 FileContentResult It returns file content
3 FilePathResult It returns file content
4 FileStreamResult It returns file content
5 EmptyResult It returns nothing
6 JavaScriptResult It returns script for execution
7 JsonResult It returns JSON formatted data
8 RedirectToResult It redirects to the specified URL
9 HttpUnauthorizedResult It returns 403 HTTP Status code
10 RedirectToRouteResult It redirects to different action or different controller action
11 ViewResult It received as a response for view engine
12 PartialViewResult It received as a response for view engine

Let’s look at a simple example from the previous chapter where an EmployeeController was created.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVCControllerDemo.Controllers
{
    public class EmployeeController : Controller
    {
        // GET: Employee
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Search(string name)
        {
            var input = Server.HtmlEncode(name);
            return Content(input);
        }
    }
}
ASP.NET MVC Actions

Add Controller

Let’s add another controller to it.

Step 1 ? Right- click on the folder Controllers and select Add to Controller.

ASP.NET MVC Actions

The Add Scaffold dialog will be displayed.

ASP.NET MVC Actions

Step 2 ? Choose the MVC 5 Controller– Empty option and then click ‘Add’. Then Add Controller dialog is shown.

ASP.NET MVC Actions

Step 3 ? Set the CustomerController name and press the button ‘Add’. You will now see a new C # file ‘ CustomerController.cs’ in the Controllers folder that is also available for editing in Visual Studio.

ASP.NET MVC Actions

Similarly, add an additional controller called HomeController. The following is the class implementation of HomeController.cs.

using System;
using System.Collections.Generic;
using System.Linq;

using System.Web;
using System.Web.Mvc;

namespace MVCControllerDemo.Controllers {
   public class HomeController : Controller{
      // GET: Home
      public string Index(){
         return "This is Home Controller";
      }
   }
}

Step 4 ? Run this application and you’re getting the next output.

ASP.NET MVC Actions

Step 5 ? In the customer controller that we created above, add the following code.

public string GetAllCustomers(){
   return @"<ul>
      <li>Jade</li>
      <li>Jazzy</li>
      <li>Allan</li>
      <li>Jerry</li>
   </ul>";
}

Step 6 ? Run the http:/localhost:2921/Customer/GetAllCustomers application and request. You ‘ll see the output below.

ASP.NET MVC Actions

You can also redirect to the same controller actions or even to another controller. The following is a simple example in which we redirect from HomeController to Customer Controller using the following code to change the code in HomeController.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVCControllerDemo.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            return RedirectToAction("GetAllCustomers", "Customer");
        }
    }
}

As you can see, we used the ActionResult method RedirectToAction(), which takes two parameters, the name of the action and the name of the controller. When you run this application, it is redirected to /Custome/GetAllCustomers by default.