January 17, 2021
Hot Topics:


  • By Jani Järvinen
  • Send Email »
  • More Articles »

If you run the skeleton application just created by pressing F5, your browser should display a page similar to that on Figure 3.

Click here for a larger image.

Figure 3: The basic ASP.NET MVC application look.

Often, the best way to learn a new technique is to see how an example application has been built. The skeleton created by the MVC project template is one such example. ASP.NET MVC applications handle the web requests thru controller classes, and the request from the browser is directed to a particular controller via routes.

Routes are a new feature in .NET 3.5 SP1, and ASP.NET MVC uses this feature to give controller classes the capability to respond to requests. ASP.NET MVC uses REST-like URLs (Representational State Transfer) that are cleaner than regular ASP.NET web application URLs. Here are examples of such URLs:


As you can see, REST-like URLs tend to be clean, simple, and don't expose .aspx files directly on the server. Although you can have directly addressed .aspx pages in ASP.NET MVC applications, this is not the main idea.

Through developer-defined routes, the ASP.NET MVC application can direct requests to controllers. Routes are defined once for the entire ASP.NET application, so the Global.asax file is the logical place to define these. In fact, if you take a look at the default Global.asax.cs file in an ASP.NET MVC application, you will see a method called RegisterRoutes defined. This method is called from the Application_Start method, and implemented like this:

public static void RegisterRoutes(RouteCollection routes)
      new { controller = "Home",
         action = "Index",
         id = "" }

By default, a route called "Default" is defined with three parts: controller, action, and finally an ID separated by slashes. When a request arrives at the MVC application, the URL is parsed according to this definition. For instance, given the request for "/products/show/881", this would be parsed so that the "controller" parameter would have the value of "products", the "action" parameter would be "show", and finally the "id" would be "881".

Note: Parameter values can be optional, in which case the route also has default values specified. For instance, if the action parameter value does not exist, the default value of "Index" is used. Similarly, "Home" is the default value for the controller. Given a request of "/" (the root), the Home controller is used with the "Index" action.

Handling Requests with Controllers

If you recall the default routing from the previous code listing, you know that in the skeleton ASP.NET MVC application, the Home controller is the default controller for requests that do not specify any controller in the request URL.

Controller classes live in the Controllers folder in the Visual Studio solution. From here, you can by default find two controller classes: the HomeController and the AccountController. The naming convention is that each controller class should have the word "Controller" in its name. This way, the MVC framework can redirect a request for "/products" for the ProductsController class.

Each of your own controller classes descends from the Controller class, which is part of the System.Web.Mvc namespace, the default namespace for all classes related to MVC. Then, each public method in your controller class can handle an action based on the method's name. For example, in the case of the previous request to "/products/show/881", the ProductsController's "Show" method would be called.

This method would be the place to write the logic to handle the request. Normally, a controller method returns a type called ActionResult (a class defined also in the System.Web.Mvc namespace), which is most commonly a view object, although it also can be something else. As you will recall, the view is the actual representation of the data, most often HTML code.

When developing your ASP.NET MVC application, you want to add our own actions and controllers. This is very easy indeed, because all you need to do is write more code! That is, if you want to add new actions such as "delete" or "insert" to your ProductsController, it is enough just to write those methods. You do not need to add anything to the routes in the Global.asax.cs file because the routing system is intelligent enough to do all this for you. The same goes with controller classes; just add your new controllers to the Controllers folder in the solution.

Returning Views to the Browser

Up to this point, you have learned about controllers. Next, you will see how controllers can manipulate a view and display data on it. Because the idea behind MVC applications is that you separate the logic from the view, it would not make much sense if the controller would construct HTML code directly to be returned to the user.

Page 2 of 4

This article was originally published on December 3, 2008

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date