Asp.Net MVC

Actions

Asp.Net MVC / Actions

Actions

Action Methods in ASP.NET MVC

 

In ASP.NET MVC, action methods are responsible for handling incoming requests and returning appropriate responses. By default, an action method returns an object of type ActionResult, which represents different kinds of responses sent back to the client.

Each action generally corresponds to a user interaction, such as:

Entering a URL in the browser

Clicking a link

Submitting a form

Whenever a user performs one of these actions, a request is sent to the server. The URL of the request contains information that helps the MVC framework determine which controller and action method should be executed.

Rules for Action Methods

Action methods must be instance methods (they cannot be static)

There is no strict restriction on return type

They can return strings, integers, views, JSON, redirects, etc.

How MVC Processes a Request

 

Let’s understand how a request like /Home/Index is handled:

Request Processing


 

The UrlRoutingModule analyzes the incoming URL.

It matches the URL with entries defined in the routing table.

Control is passed to the MVC Route Handler.

The route handler forwards the request to MvcHandler, which is an HTTP handler.

MvcHandler uses a controller factory to create the appropriate controller.

The controller is identified using data stored in RouteData.

The controller’s Execute() method is called.

An Action Invoker is created to locate the correct action method.

The action method is executed and produces a response.

Types of Action Results

 

ActionResult is the base class for all result types. Common action results include:

No.Action Result TypeDescription
1ContentResultReturns plain text or string
2FileContentResultReturns file content
3FilePathResultReturns file from a path
4FileStreamResultReturns file as a stream
5EmptyResultReturns no response
6JavaScriptResultReturns JavaScript code
7JsonResultReturns JSON-formatted data
8RedirectResultRedirects to a URL
9HttpUnauthorizedResultReturns HTTP 403 status
10RedirectToRouteResultRedirects to another route
11ViewResultReturns a view
12PartialViewResultReturns a partial view

Example

Lets have a look at a simple example from the previous chapter in which we have created an EmployeeController.

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 Search(string name){         var input = Server.HtmlEncode(name);         return Content(input);      }   } }

When you request the following URL http://localhost:61465/Employee/Mark, then you will receive the following output as an action.

Localhost Employee Mark Output


 

Add Controller

 

Let us add one another controller.

Step 1 − Right-click on Controllers folder and select Add → Controller.

Add Another Controller

It will display the Add Scaffold dialog.

Add Scaffolding Dialog

Step 2 − Select the MVC 5 Controller Empty option and click Add button.

The Add Controller dialog will appear.

CustomerController

Step 3 − Set the name to CustomerController and click Add button.

Now you will see a new C# file CustomerController.cs in the Controllers folder, which is open for editing in Visual Studio as well.

set_name CustomerController

Similarly, add one more controller with name HomeController. Following is the HomeController.cs class implementation.

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 will receive the following output.

Home Controller Output

Step 5 − Add the following code in Customer controller, which we have created above.

public string GetAllCustomers(){   return @"

         
  • Ali Raza
  •      
  • Mark Upston
  •      
  • Allan Bommer
  •      
  • Greg Jerry
  •  
"; }

Step 6 − Run this application and request for http://localhost:61465/Customer/GetAllCustomers. You will see the following output.

Localhost GetAllCustomers

You can also redirect to actions for the same controller or even for a different controller.

Following is a simple example in which we will redirect from HomeController to Customer Controller by changing the code in HomeController using the following code.

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 have used the RedirectToAction() method ActionResult, which takes two parameters, action name and controller name.

When you run this application, you will see the default route will redirect it to /Customer/GetAllCustomers

Localhost Customers GetAllCustomers


 

Technology
Asp.Net MVC
want to connect with us ?
Contact Us