Actions in ASP.Net Web API

Introduction

This article explains actions in the ASP.NET Web API. Action methods returns the response of the user input and it also returns an action result.

ActionResult

ActionResult is a class, all the action methods depend on the ActionResult class, so it is called the base of the action method. An ActionResult redirects to the other controller's method. There are various types of action results that depend on the task to be performed by the action method. The common method called by the action is the "View method".

The following are the various types derived from the ActionResult:

  • ViewResult: It returns a view as a web page.
  • PartialViewResult: It returns a partial view. The PartialView defines the fragment of the view that this view gives inside another view.
  • RedirectToActionResult: It redirects to another action method of the other controller.
  • ContentResult: It returns the user defined content.
  • FileResult: It returns the binary file content for writing the response.
  • EmptyResult: It represents the return value used for the action method that does nothing.
  • JSONReturn: It returns the object of the serialized JSON.
  • RedirectResult: It redirects to another action method using a URL.

Let's see an example of ActionResult

First create an application as in the following:

  • Start Visual Studio 2012.
  • From the start window select "New Project".

  • In the Template Window select "Installed" -> "Visual C#" -> "Web".

  • Select "ASP.NET MVC 4 Web Application" and click on "OK".

    a6.jpg

  • From the "MVC4 Project" window select "Web API".

    a7.jpg

Now we use the "RedirectToAction":

redirect to action method is used for redirecting to the method of the other controller. Here we create two controllers, the first one is "HomeController" and the second is "DemoController".

The "HomeController.cs" exists:

  • In the Solution Explorer
  • Select "Controller folder" -> "HomeController"

HomeController code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. namespace Action.Controllers  
  7. {  
  8.     public class HomeController : Controller  
  9.     {  
  10.         public ActionResult Index()  
  11.         {  
  12.             ViewBag.Message = "ASP.NET Web API";  
  13.             return View();  
  14.         }  
  15.         public ActionResult Clearify()  
  16.         {  
  17.             return RedirectToAction("Clearify""Demo");  
  18.         }  
  19.     }  
  20. }   

In the code above the "RedirectToACtion" metod redirects the "Clearify" method of the "DemoController".

The "DemoController.cs" is created:

  • In the Solution Explorer.

  • Right-click on the "Controller folder" -> "Add" -> "Controller".

    a5.jpg

  • Name it "DemoController" and click on the "Ok" button.

The "DemoController" code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. namespace Action.Controllers  
  7. {  
  8.     public class DemoController : Controller  
  9.     {  
  10.         public ActionResult Index()  
  11.         {  
  12.             return View();  
  13.         }  
  14.         public ActionResult Clearify()  
  15.         {  
  16.             return Content("Hi, It is a Web APi Action");  
  17.         }  
  18.     }  
  19. } 

Now we write some lines of code in the index.cshtml file that exists in "Solution Explorer" -> "Views" -> "Home" -> "Index.cshtml".

Code:

 

  1. @{ViewBag.Title = "Index";}  
  2. <h2> Web API Index</h2>  
  3. @ViewBag.message  
  4. <a href="/Home/Clearify">Click on link to Clearify action</a>  

 

Now execute the application by pressing F5.

a.jpg


Click on the link, then this line will be executed from the HomeController Clearify action.

  1. return RedirectToAction("Clearify""Demo");  

And then the output will be:

a1.jpg

  1. Routers

It is a technique that is used for constructing the URL for the Web request. In the MVC applications the URL contains the controller and action name.

In the Global.asax file add the RegisterRoutes method for the default routing work. This file exists in the "SolutionExplorer" -> "Global.asax".

Add the method:

  1. public static void RegisterRoutes(RouteCollection routes)  
  2. {  
  3.     routes.IgnoreRoute("{resource}.axd/{*pathinfo}");  
  4.         routes.MapRoute(  
  5.             "Default",  
  6.             "{controller}/{action}/{id}",  
  7.             new  
  8.             {  
  9.                 controller="Home",  
  10.                 action="Index",  
  11.                 id=UrlParameter.Optional  
  12.             }  
  13.         );  
  14. } 

In the code above we route the URL with the "{Controller}/{action}/{id}". In it id is the optional parameter. In this URL, first parameter controller is for the controller name, the second is action for the action name and the last parameter is for the id.

In the "index.cshtml" file write this code:

 

  1. @{ViewBag.Title = "Index";}  
  2. <h2>  Web APi Index</h2>  
  3. @ViewBag.message  
  4. <a href="/Demo/Clearify">Click on link to Clearify action</a>  

 

Again execute the application.

a2.jpg

Click on the link.

a3.jpg

Now again go to the Global.asax.cs file.  Add the following code to the RegisterRoutes(RouteCollection routes) method:

  1. public static void RegisterRoutes(RouteCollection routes)  
  2. {  
  3.     routes.MapRoute(  
  4.                   "Demo",  
  5.                   "Demo/{action}",  
  6.                   new  
  7.                   {  
  8.                      controller = "Demo",  
  9.                       action = "Clearify",  
  10.                       id = UrlParameter.Optional  
  11.                   });  
  12. }   

Again execute the application:

a4.jpg


Similar Articles