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

Popular posts from this blog

Classes in C# Explained

C# Class Explained A class is nothing but an encapsulation of properties and methods that are used to represent a real-time entity, as explained by Guru99 . For instance, if you want to work with Guest’s data as in our previous DataDriven Web application . The properties of the Guest would be the Id, GuestName, Address, Phone number etc of the Guest. The methods would include the entry and modification of Guest data. All of these operations can be represented as a class in C# as shown below. using System; namespace CsharpnaijaClassTutorial {     public class Guest     {         public int Id { get ; set ; }         public string GuestName { get ; set ; }         public string Address { get ; set ; }         public string WhomToSee { get ; set ; }     ...

ASP.NET MVC Views

Views in ASP.NET MVC Application explained Find a related article By  Steve Smith  and  Luke Latham from Microsoft Corporation here In the Model-View-Controller (MVC) pattern, the  view  handles the application's data presentation and user interaction. A view is an HTML template with embedded  Razor markup . Razor markup is code that interacts with HTML markup to produce a webpage that's sent to the client. In ASP.NET MVC, views are  .cshtml  files that use the  C# programming language  in Razor markup. Usually, view files are grouped into folders named for each of the application's  controllers . The folders are stored in a  Views  folder at the root of the application as shown: The  Home  controller is represented by a  Home  folder inside the  Views  folder.  The  Home  folder contains the views for the  About ,  Contact , and  Index...

ASP.NET MVC Routing

ASP.NET MVC Routing ASP.NET MVC routing is a pattern matching system that is responsible for mapping incoming browser requests to specified MVC controller actions. When the ASP.NET MVC application launches then the application registers one or more patterns with the framework's route table to tell the routing engine what to do with any requests that matches those patterns. When the routing engine receives a request at runtime, it matches that request's URL against the URL patterns registered with it and gives the response according to a pattern match. Routing pattern is as follows A URL is requested from a browser, the URL is parsed (that is, break into controller and action), the parsed URL is compared to registered route pattern in the framework’s route table, if a route is found, its process and send response to the browser with the required response, otherwise, the HTTP 404 error is send to the browser. Route Properties ASP.NET MVC routes are res...