Chapter 4. Controller basics

 

This chapter covers

  • Understanding the controller anatomy
  • Storyboarding an application
  • Mapping the presentation model
  • Using input from the browser
  • Passing view metadata
  • Testing the controller

The focus of the Model-View-Controller pattern is the controller. With this pattern, every request is handled by a controller and rendered by a view. Without the controller, presentation and business logic would move to the view, as we’ve seen with Web Forms.

With the ASP.NET MVC Framework, every request routes to a controller, which is simply a class that implements the IController interface. Microsoft provides the base class System.Web.Mvc.Controller to make creating a controller easy. Which controller base class you choose isn’t crucial because most request processing goes into executing the ActionResult, which is the type that each action returns.

An action is a method on the controller class that handles a particular request. This method can take zero or many parameters, but by the time the action method finishes executing, there ought to be one or many objects ready to be sent to the view, and the name of the view should be selected if the view doesn’t follow the convention of having the same name as the action. Beyond that, the developer is in complete control when implementing a controller and its actions.

4.1. The anatomy of a controller

4.2. Storyboarding an application

4.3. Transforming a model to a view model

4.4. Accepting input

4.5. Testing controllers

4.6. Summary

sitemap