Skip to main content

Actions in ASP.NET MVC


Actions in ASP.NET MVC Explained


ASP.NET MVC Actions are methods in an ASP.NET MVC controllers responsible for executing requests and generate responses to it. By default, it generates a response in the form of ActionResult. Actions typically have a one-to-one mapping with user interactions.
For example, enter a URL into the browser, click on any particular link, and submit a form, etc. Each of these user interactions causes a request to be sent to the server. In each case, the URL of the request includes information that the MVC framework uses to invoke an action method in a controller class.

Characteristics of action methods

ü Must be an instance method
ü Must not be a static method
ü No return value restrictions, that is, it can return string, Boolean or integer etc.

Request Processing

Actions are the final request destination in an MVC application and it uses the controller base class. Let's take a look at the request processing.
·        When a URL arrives, like /Employee/Index, it is the UrlRoutingModule that inspects and understands that something configured within the routing table knows how to handle that URL.


·        The UrlRoutingModule puts together the information we've configured in the routing table and hands over control to the MVC route handler.
·        The MVC route handler passes the controller over to the MvcHandler which is an HTTP handler.
·        MvcHandler uses a controller factory to instantiate the controller, the handler knows what controller to instantiate because it looks in the RouteData for that controller value.
·        Once the MvcHandler has a controller, the only thing that MvcHandler knows about is IController Interface, so it simply tells the controller to execute.
·        When it tells the controller to execute, that's been derived from the MVC's controller base class. The Execute method creates an action invoker and tells that action invoker to go and find a method to invoke, that is, an action to invoke.
·        The action invoker, again, looks in the RouteData and finds that action parameter that's been passed along from the routing engine.

 Types of Action

Actions basically return different types of action results. The ActionResult class is the base for all action results. Following is the list of different kind of action results and its behavior.

Action
Return Types
ContentResult
Returns a string
FileContentResult
Returns file Content
FilePathResult
Returns file content
FileStreamResult
Returns File content
EmptyResult
Returns nothing
JavaScriptResult
Returns script for execution
JsonResult
Returns JSON formatted data
RedirectToResult
Redirects to the specified URL
HttpUnauthorizedResult
Returns 403 HTTP status code
RedirectToRouteResult
Redirects to different action or different controller action
ViewResult
Received as a response for view engine
PartialViewResult
Received as a response for view engine

Let’s have a look at a simple example from the previous article in which we have created an EmployeesController.
using DatabaseFirstApproach.Models;
using System.Configuration;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web.Mvc;

namespace DatabaseFirstApproach.Controllers
{
    public class employeesController : Controller
    {
        private ApplicationDbContext db = new ApplicationDbContext();

        // GET: employees
        public ActionResult Index()
        {
            var employees = db.employees.Include(e =>                        e.department);
            return View(employees.ToList());
        }
   }
}
From the above code snippet, we can see that we have a controller class that inherits a base class called Controller and we have one action method called Index which returns an ActionResult. We wrote a code block to display all employees and their respective departments.

When we request the following URL http://localhost: 44355/Employees/Index, then we will receive the following output as an action.


Points to Remember

1.     Actions are methods in an ASP.NET MVC Controller
2.     Actions cannot be static method
3.     Must be an instance method
4.     Actions don’t have restriction for return type

Learn more from tutorial Point here

Do you want to be a software developer using C# programming language and start earning good salary within three months at a give-away price? Try our online project-based software Development hands-on training.

Contact the following for more info
Name: Musa Sule Gadabs

Phone Number: 08032299383

Comments

Post a Comment