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

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...

Most Popular Programming Languages in 2020

Most Popular Programming Languages in 2020 In this blog post, you will learn about the most popular programming languages in 2020 for creating the best web applications. Check its pros and cons. Analyzed by technostacks Not very long ago, just a few people were considered to be computer programmers, and the general public viewed them with awe. In this digital age that we are now living in, however, a large number of IT jobs need a solid grasp of one or more programming languages. Whether one wants to develop a mobile app or get a certification for having programming knowledge, or even to learn new skills, one needs to opt for the right programming language. Below mentioned eight most popular programming languages which are in demand for software development and web applications. This is the most used programming languages in 2019 and will be in 2020. For each, there is little information about the language, benefits and its complexity, as well as about its usage. One must...

HashTable in C# with Example

  HashTable in C# with Example Hashtable  is used to store a collection of key/value pairs of different  data types  and are organized based on the hash code of the key.   Generally, the hashtable object will contain buckets to store elements of the collection. The bucket here, is a virtual subgroup of elements within the hashtable and each bucket is associated with a hash code, which is generated based on the key of an element.   In C#, hashtable is same as a  dictionary  object but the only difference is that the  dictionary  object is used to store a key-value pair of same  data type  elements.   When compared with  dictionary  object, the hashtable will provide a lower performance because the hashtable elements are of object type so the boxing and unboxing process will occur when we are storing or retrieving values from the hashtable.   C# HashTable Declaration Hashtable is a non-generic type...