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

  • Mastering Blazor Authentication and Authorization

    At the Visual Studio Live! @ Microsoft HQ developer conference set for August, Rockford Lhotka will explain the ins and outs of authentication across Blazor Server, WebAssembly, and .NET MAUI Hybrid apps, and show how to use identity and claims to customize application behavior through fine-grained authorization.

  • Linear Support Vector Regression from Scratch Using C# with Evolutionary Training

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the linear support vector regression (linear SVR) technique, where the goal is to predict a single numeric value. A linear SVR model uses an unusual error/loss function and cannot be trained using standard simple techniques, and so evolutionary optimization training is used.

  • Low-Code Report Says AI Will Enhance, Not Replace DIY Dev Tools

    Along with replacing software developers and possibly killing humanity, advanced AI is seen by many as a death knell for the do-it-yourself, low-code/no-code tooling industry, but a new report belies that notion.

  • Vibe Coding with Latest Visual Studio Preview

    Microsoft's latest Visual Studio preview facilitates "vibe coding," where developers mainly use GitHub Copilot AI to do all the programming in accordance with spoken or typed instructions.

  • Steve Sanderson Previews AI App Dev: Small Models, Agents and a Blazor Voice Assistant

    Blazor creator Steve Sanderson presented a keynote at the recent NDC London 2025 conference where he previewed the future of .NET application development with smaller AI models and autonomous agents, along with showcasing a new Blazor voice assistant project demonstrating cutting-edge functionality.

Subscribe on YouTube