Asp.Net Mvc Controllers Overview

Controller Overview

The ASP.NET MVC framework maps URLs to classes that are referred to as controllers.


  • Process incoming requests
  • Handle user input and interactions and
  • Execute appropriate application

The Controller class inherits from ControllerBase and is the default implementation of a controller. The Controller class is responsible for the following processing stages:

  1. Locating the appropriate action method to call and validate that it can be
  2. Getting the values to be used as the action method’s
  3. Handling all errors that might occur during the execution of the action
  4. Providing the View for rendering ASP.NET pages to

ActionMethods and ActionResult object

ASP.NET MVC application is organized around controllers and action methods. The controller defines action

methods. Controllers can include as many action methods as needed.

  • Action methods typically have a one-to-one mapping with user interactions. Examples of user interactions include entering a URL into the browser, clicking a link, and submitting a Each of these user interactions causes a request to be sent to the server. In each case, the URL of the request includes information that the MVC framework uses to invoke an action method.
  • Most action methods return an instance of a class that derives from ActionResult. 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 The View method returns an instance of the ViewResult class, which is derived from ActionResult.
  • We 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

The following table shows the built-in action result types and the action helper methods that return them:

ActionResult Inherited Classes Action Method Description
ViewResult View

Renders a view as a Web page.

PartialViewResult PartialView Renders a partial view, which defines a section of a

view that can be rendered inside another view.

RedirectResult Redirect


Redirects to another action method by using its URL.
RedirectToRouteResult RedirectToAction


Redirects to another action method.
ContentResult Content Returns a user-defined content type.

FilePathResult FileStreamResult

File Returns binary output to write to the response.
JavascriptResult JavaScript Return JavaScript content
JsonResult Json Returns a serialized JSON object.
EmptyResult null Returns Empty Page.
HttpNotFoundResult HttpNotFound Returns 404 error.
HttpStatusCodeResult Returns the specified Status code and Description

Return Unauthorized Http response

Example of ViewResult

By default, the Controller actions will return the ActionResult object. We can return various types of results as ActionResult, which will decide how the output needs to render on the browser.

Example of ViewResult

Example of ContentResult.

Example of Redirect

Example of RedirectToAction: Depending on the input values, we can redirect to another Action.

Example of RedirectToRoute

When we need to redirect to a route defined in Global.asax, we will use the RedirectToRoute object. In Global.asax:

routes.MapRoute(“RenderXml”, // Route name

“Home/RenderXml”);//, // URL with parameters

Example of File

File is used to return the content of a file to the browser.

Example of JavaScript:

We are render javascript library functions (like a .js file)

Example of JSON

We can render the text to the result page or can send it as a file to the client using JSON notation.

Non-Action Methods:

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.




Leave a Reply

Your email address will not be published. Required fields are marked *