Posted by: Cirilo Meggiolaro | 04/13/2009

Tip of the day #181 – ASP.NET MVC – Controllers

On Tip #179 I made an overview of ASP.NET MVC. Yesterday the Tip #180 described the routing model of ASP.NET MVC.

It’s impossible to talk about the routing model without hit the controllers. Basically the controllers are the orchestrators of the MVC. They are responsible for accessing the model objects and invoking views or returning specific content as we will see further. The controller class may contain methods also known as actions that are triggered when a URL is requested. Let’s rewind a little bit.

We saw yesterday that when a URL similar to http://localhost/Account/Register is requested, the runtime will use the rules described on the route table to resolve the request and trigger an action method called Register on a controller class called Account.

Naming Conventions

An important naming convention for controller classes is the class name itself. All controller classes must have the suffix Controller attached to its name. On the URL http://localhost/Account/Register the controller class name is called AccountController.

All controller classes inherit from the base System.Web.Mvc.Controller class.

Since you have a controller class, you may start adding your action methods.

Action Methods

Action methods are public methods that return an ActionResult type. The ActionResult class is an abstract type and there are several types that inherit from it. These types are not usually instantiated on the action method scope but some methods on the base controller class wrap the creation of those objects. The method names available are shown below within parenthesis:

  • ContentResult (Content) – Used to return a string containing the result;
  • EmptyResult – No action result is retrieved;
  • JavaScriptResult (JavaScript) – Used to return a JavaScript script;
  • JsonResult (Json) – Used to return a string using Json format;
  • FileContentResult (File) – Used to return the content of a downloadable file;
  • FilePathResult (File) – Used to return the path of a downloadable file;
  • FileStreamResult (File) – Used to return a file stream of a downloadable file;
  • RedirectResult (Redirect / RedirectToAction / RedirectToRoute) – Used to redirect to a different URL;
  • ViewResult (View)- The ViewResult type is used to “load” a view using html.

Most commonly used the ViewResult invokes a View to be loaded. Let’s check the following code snippet:

public ActionResult Index()
    return View();

The action method above requests a view called Index to be loaded. When the parameterless overload is requested a view with a name equals to the action method name is loaded. The following overloads are available:

  • ViewResult View();
  • ViewResult View(IView view);
  • ViewResult View(object model);
  • ViewResult View(string viewName);
  • ViewResult View(IView view, object model);
  • ViewResult View(string viewName, object model);
  • ViewResult View(string viewName, string masterName);
  • ViewResult View(string viewName, string masterName, object model);

If you want to invoke a View with a different name and pass a model object you may use the following syntax:

public ActionResult Page()
    return View(“ProductList”, productModel);

The following action snippet returns a message if the product is out of stock. You must use the Content method on the base Controller class to achieve that:

public ActionResult Locate(int productID)
    /// If the product is not available, retrieves a message.
    return Content(“The product has sold out.”);

Tomorrow we are going to explore the model objects.

Stay tuned!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: