Practical ASP.NET

Controlling Controllers in ASP.NET MVC

Peter Vogel begins his series on introducing ASP.NET developers to ASP.NET MVC by looking at the C in MVC: Controllers

An ASP.NET MVC application consists of three components, obviously: Models, Views and Controllers. The Models are the middle-tier business objects that you might use with any application -- I'll come back to them later. The Controllers, on the other hand, are peculiar to the ASP.NET MVC environment and are critical to understanding the structure (and, therefore, the benefits) of building an ASP.NET MVC application.

Instead of requesting a page, the browser now really calls a Controller. You need to decide, early in the request cycle, which method on which controller a request from the Web browser should call -- a process called routing. I've discussed routing before in .NET 4. Fundamentally, you build a table with code in the Global.asax file that ties a method on a controller to a particular URL. While this is additional work not required by a traditional ASP.NET application, as I discussed in that earlier column, the flexibility and reduction in maintenance costs that result in using routing more than offset the work involved.

Controller methods are tremendously flexible. From a controller method (called an Action, a term that I'll use from here on because it's short) you return an ActionResult. An ActionResult can be a redirect to another page, a JSON object (useful for returning results to AJAX-enabled pages), or a View result, which results in a page. From your code in the Action you can also access any parameter data that was embedded in the URL that caused your Action to be called.

So, for instance, if the URL sent from the browser (or whatever client issues the request) is http://myserver.com/Customer/A123/Order/History/2009, then you might call an Action that displays the customer's order history. Within the controller you'd be able to access the data embedded in the URL: The customer number (A123) and the year to display (2009). Retrieving the data would be a matter of calling the right methods on the right business objects (the M for Model in MVC).

To create an ASP.NET MVC application, I used the ASP.NET MVC 2 template in Visual Studio 2010 (for earlier versions of Visual Studio, you'll need to download and install the ASP.NET MVC package) and selected the testing tool I wanted to use. I set up a routing table in my Global.asax file (again, see the earlier article) and then added a new Controller to the application (controllers are kept in the Controllers folder that is part of the project's template). Since this controller was going to handle all activities around customer history, I called it CustomerHistoryController.

An aside here: The flexibility of the controller methods means that it's worth investigating a Service Oriented Architecture design process. Because a controller can return such a wide variety of results, it really acts like a service: A Web page is just one output from this service. For SOA design, I follow a lite version of Michael Bell's Service Oriented Modeling Framework. Using that process led me to creating a CustomerHistoryController with an operation (Action) that returns a Web page for this particular request.

Back to the code: Within my controller I created a Controller called PurchasingByYear that expects to be passed a CustomerId and a Year:

Namespace Sample_MVC
    Public Class CustomerHistoryController
        Inherits System.Web.Mvc.Controller

  Public Function PurchasingByYear(ByVal CustomerNum As String, 
ByVal Year As Date) As ActionResult

	'..code to retrieve purchasing data

      Return View()
  End Function
End Class
End Namespace

An Action returns a View of the same name, so my next step was to add a View called PurchasingByYear to the Views folder. That means I have to discuss what a View is, which I'll do in my next column.

About the Author

Peter Vogel is a system architect and principal in PH&V Information Services. PH&V provides full-stack consulting from UX design through object modeling to database design. Peter tweets about his VSM columns with the hashtag #vogelarticles. His blog posts on user experience design can be found at http://blog.learningtree.com/tag/ui/.

comments powered by Disqus

Featured

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube