Understanding Controllers and Actions

Published on by Inno Design

Hello Guys,

It has been so long we wrote on Asp.Net MVC….Sorry for the dis-continuity…But here we are , lets understand the controllers and actions…

ASP.NET MVC controllers are responsible for controlling the flow of application execution. When you make a browser request against an ASP.NET MVC application, a controller is responsible for returning a response to that request.

Controllers expose one or more actions. A controller action can return different types of action results to a browser.

For example

–> a controller action might return a view,

–> a controller action might return a file, or

–> a controller action might redirect you to another controller action.

Creating a Controller

The easiest way to create a controller is to right-click the Controllers folder in the Visual Studio Solution Explorer window and select the menu option Add, Controller.

Selecting this menu option displays the Add Controller dialog :


If you enter the name PersonController, you get the code in below listing :



Note : A controller name must end with the suffix Controller. If you forget to include the Controller suffix, you can’t invoke the controller.

Notice that a controller is just a class (a Visual Basic or C# class) that inherits from the base System.Web.Mvc. Controller class. The controller class in above Listing exposes one action named Index(). The Index() action is
the default action that is invoked on a controller when no explicit action is specified.

Notice that the Index() action returns an ActionResult. A controller action always returns an ActionResult (even if it doesn’t appear to be returning an ActionResult). The ActionResult determines the response returned to the browser. The Index() controller returns a view as its ActionResult.

A controller typically exposes multiple actions. You add actions to a controller by adding new methods to the controller. For example, the modified Person controller in the below Listing exposes three actions named Index() , Help(), and Details() .


Here’s what you would type into a browser address bar to invoke the different actions:

  • /Person/Index — Invokes the PersonController Index( ) action
  • /Person — Invokes the PersonController Index() action
  • /Person/Help — Invokes the PersonController Help() action
  • /Person/Details/34 — Invokes the PersonController Details() action with the value 34 for the Id parameter

You invoke a controller action by following a particular pattern that looks like this:

Notice that when you invoke a controller, you don’t include the Controller suffix in the URL. For example, you invoke the Person controller with the URL /Person/Index and not the URL /PersonController/Index.

The default controller action is the Index() action. Therefore, the URL /Person/Index and the URL /Person both invoke the Person controller Index( ) action.

When you invoke a controller, you can supply an optional Id parameter. For example, the Details() action accepts an Id parameter. The URL /Person/Details/2 invokes the Details() action and passes the value 2 for the Id parameter. The name of the parameter is important. You must name the parameter Id.

Note : The default pattern for invoking controller actions is defined by the default route in the Global.asax file. If you want to modify the URL pattern for invoking actions, you canmodify this default route. we will learn more about creating custom routes when we will discuss Routing.

Returning Action Results

A controller action always returns an ActionResult. The ASP.NET MVC framework includes the following types of ActionResults.

  • ViewResult — Represents an ASP.NET MVC view.
  • PartialViewResult — Represents a fragment of an ASP.NET MVC view.
  • RedirectResult — Represents a redirection to another controller action or URL.
  • ContentResult — Represents raw content sent to the browser.
  • JsonResult — Represents a JavaScript Object Notation result (This is useful in Ajax scenarios).
  • FileResult — Represents a file to be downloaded.
  • EmptyResult — Represents no result returned by an action.
  • HttpUnauthorizedResult — Represents an HTTP Unauthorized status code.
  • JavaScriptResult — Represents a JavaScript file.
  • RedirectToRouteResult — Represents a redirection to another controller action or URL using route values.

Typically, you don’t directly return an ActionResult from a controller action. Instead, you call a controller method that returns an ActionResult. For example, if you want to return a ViewResult, you call the controller View() method.
Here’s a list of controller methods that return ActionResults :

  • View( ) — Returns a ViewResult
  • PartialView() — Returns a PartialViewResult
  • RedirectToAction() — Returns a RedirectToRouteResult
  • Redirect() — Returns a RedirectResult
  • Content() — Returns a ContentResult
  • Json( ) — Returns a JsonResult
  • File( ) — Returns a FileResult
  • JavaScript() — Returns a JavaScriptResult
  • RedirectToRoute( ) — Returns a RedirectToRouteResult

we will use these all methods in our programming further….

The goal of this post was to provide depth explanation of how you can create controllers and controller actions. here you were provided with an overview of the different types of ActionResults that can be returned from a controller action.

Enjoyed ?? This was all about Controller and We have tried our best to explain……

Hope this helps….

Published on ASP.NET MVC

Comment on this post