Controllers & Action Methods in ASP.NET MVC Applications

The ASP.NET MVC framework maps URLs to classes that are referred to as controllers. Controllers process incoming requests, handle user input and interactions, and execute appropriate application logic. A controller class typically calls a separate view component to generate the HTML markup for the request.

The base class for all controllers is the ControllerBase class, which provides general MVC handling. The Controller class inherits from ControllerBase and is the default implement of a controller. 

The Controller class is responsible for the following processing stages: 
  • Locating the appropriate action method to call and validating that it can be called.
  • Getting the values to use as the action method's arguments.
  • Handling all errors that might occur during the execution of the action method.
  • Providing the default WebFormViewEngine class for rendering ASP.NET page types (views).
All controller classes must be named by using the "Controller" suffix. If not then mvc framework not able to recognize controller.
In ASP.NET applications that do not use the MVC framework, user interaction is organized around pages, and around raising and handling events from the page and from controls on the page. In contrast, user interaction with ASP.NET MVC applications is organized around controllers and action methods. The controller defines action methods. Controllers can include as many action methods as needed, i. e. When a user enters a URL into the browser, the MVC application uses routing rules that are defined in the Global.asax file to parse the URL and to determine the path of the controller. The controller then determines the appropriate action method to handle the request. 
By default, the URL of a request is treated as a sub-path that includes the controller name followed by the action name. For example, if a user enters the URL http://TestProduct.com/MyWebSite/Products/Categories, the sub-path is/Products/Categories. The default routing rule treats "Products" as the prefix name of the controller, which must end with "Controller" (such as ProductsController). It treats "Categories" as the name of the action. Therefore, the routing rule invokes the Categories method of the Products controller in order to process the request. If the URL ends with /Products/Detail/5, the default routing rule treats "Detail" as the name of the action, and the Detail method of the Products controller is invoked to process the request. By default, the value "5" in the URL will be passed to the Detail method as a parameter.
You can also embed parameter values as part of the URL instead of as query-string values. For example, instead of using the URL with a query string such as/Products/Detail?id=3, you can use a URL like /Products/Detail/3. The default route-mapping rule has the format /{controller}/{action}/{id}.

ActionResult Return Type :-In MVC you might see, most action methods return an instance of a class that derives from ActionResult, i.e. they rerun view().The ActionResult class is the base for all action results. However, there are different action result types, depending on the task that the action method is performing. For example, the most common action is to call the View method. The View method returns an instance of the ViewResult class, which is derived from ActionResult.

You can create action methods that return an object of any type, such as a string, an integer, or a Boolean value. These return types are wrapped in an appropriate ActionResult type before they are rendered to the response stream.


By default, the MVC framework treats all public methods of a controller class as action methods. If your controller class contains a public method and you do not want it to be an action method, you must mark that method with the NonActionAttribute attribute.

Comments