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".
From the "MVC4 Project" window select "Web API".
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:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- namespace Action.Controllers
- {
- public class HomeController : Controller
- {
- public ActionResult Index()
- {
- ViewBag.Message = "ASP.NET Web API";
- return View();
- }
- public ActionResult Clearify()
- {
- return RedirectToAction("Clearify", "Demo");
- }
- }
- }
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".
Name it "DemoController" and click on the "Ok" button.
The "DemoController" code:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- namespace Action.Controllers
- {
- public class DemoController : Controller
- {
- public ActionResult Index()
- {
- return View();
- }
- public ActionResult Clearify()
- {
- return Content("Hi, It is a Web APi Action");
- }
- }
- }
Now we write some lines of code in the index.cshtml file that exists in "Solution Explorer" -> "Views" -> "Home" -> "Index.cshtml".
Code:
- @{ViewBag.Title = "Index";}
- <h2> Web API Index</h2>
- @ViewBag.message
- <a href="/Home/Clearify">Click on link to Clearify action</a>
Now execute the application by pressing F5.
Click on the link, then this line will be executed from the HomeController Clearify action.
- return RedirectToAction("Clearify", "Demo");
And then the output will be:
-
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:
- public static void RegisterRoutes(RouteCollection routes)
- {
- routes.IgnoreRoute("{resource}.axd/{*pathinfo}");
- routes.MapRoute(
- "Default",
- "{controller}/{action}/{id}",
- new
- {
- controller="Home",
- action="Index",
- id=UrlParameter.Optional
- }
- );
- }
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:
- @{ViewBag.Title = "Index";}
- <h2> Web APi Index</h2>
- @ViewBag.message
- <a href="/Demo/Clearify">Click on link to Clearify action</a>
Again execute the application.
Click on the link.
Now again go to the Global.asax.cs file. Add the following code to the RegisterRoutes(RouteCollection routes) method:
- public static void RegisterRoutes(RouteCollection routes)
- {
- routes.MapRoute(
- "Demo",
- "Demo/{action}",
- new
- {
- controller = "Demo",
- action = "Clearify",
- id = UrlParameter.Optional
- });
- }
Again execute the application: