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

Collections in C#

Collections in C# In our previous article , we have learned about how we can use arrays in C#. Arrays in programming are used to group a set of related objects. So one could create an array or a set of Integers, which could be accessed via one variable name. What is Collections in C#? Collections are similar to Arrays, it provides a more flexible way of working with a group of objects. In arrays, you would have noticed that you need to define the number of elements in an array beforehand. This had to be done when the array was declared. But in a collection, you don't need to define the size of the collection beforehand. You can add elements or even remove elements from the collection at any point of time. This article will focus on how we can work with the different collections available in C#. There are three distinct collection types in C#: standard generic concurrent The standard collections are found under the System.Collections. They do not store elemen...

The String.Join Method in C# Explained

The String.Join Method in C#   The string.Join concatenates the elements of a specified array or the members of a collection, using the specified separator between each element or member. Overloads of string.Join Method Description Join(Char, Object[]) Concatenates the string representations of an array of objects, using the specified separator between each member. Join(Char, String[]) Concatenates an array of strings, using the specified separator between each member. Join(String, IEnumerable<String>) Concatenates the members of a constructed IEnumerable<T> collection of type String, using the specified separator between each member. Join(String, Object[]) Concatenates the elements of an object array, using the specified separator between each element. Join(String, String[]) Concatenates all the elements of a string array, usi...

System.IO Namesapce in C#

  System.IO Namesapce in C# A  file  is a collection of data stored in a disk with a specific name and a directory path. When a file is opened for reading or writing, it becomes a  stream . The stream is basically the sequence of bytes passing through the communication path. There are two main streams: the  input stream  and the  output stream . The  input stream  is used for reading data from file (read operation) and the  output stream  is used for writing into the file (write operation). From the above definition of file, the C# provides a namespace that enable us to manipulate file in C# called System.IO.   System.IO  is a  namespace  and it contains a standard IO (input/output) types such as classes , structures , enumerations , and  delegates  to perform a read/write operations on different sources like file, memory, network, etc.   System.IO Classes The table below shows differen...