Exercise 2: Creating a Controller

In this exercise, you will learn how to create a Controller to implement simple functionality of the Music Store. That controller will define action methods to handle each specific request:

  • A listing page of the music genres in the Music Store
  • A browse page that lists all of the music albums for a particular genre
  • A details page that shows information about a specific music album

For the scope of this exercise, you will have those actions to simply return a string by now.

Task 1 – Adding a New StoreController Class

In this task, you will add a new Controller.

  1. If not already open, start Microsoft Visual Web Developer 2010 Express from Start | All Programs | Microsoft Visual Studio 2010 Express | Microsoft Visual Web Developer 2010 Express.
  2. In the File menu, choose Open Project. In the Open Project dialog, browse to Source\Ex02-CreatingAController\Begin, select MvcMusicStore.sln and click Open. Alternatively, you may continue with the solution that you obtained after completing the previous exercise.
  3. Add the new controller. To do this, right-click the Controllers folder within the Solution Explorer, select Add and then the Controller command. Change the ControllerName to StoreController, and click Add.

    Figure 19

    Add Controller Dialog

Task 2 – Modifying the StoreController’s Actions

In this task, you will modify Controller methods that are called actions. These actions are responsible for responding to URL requests and determine the content that should be sent back to the browser or user that invoked the URL.

  1. The StoreController class already has an Index method. You will use it later in this Lab to implement the page that lists all genres of the music store. For now, just replace the Index method with the following code that returns a string “Hello from Store.Index()”:

    (Code Snippet – ASP.NET MVC 3.0 Fundamentals – Ex2 StoreController Index – CSharp)

    C#

    public string Index() { return "Hello from Store.Index()"; }

    (Code Snippet – ASP.NET MVC 3.0 Fundamentals – Ex2 StoreController Index – VB)

    Visual Basic

    Public Function Index() As String Return "Hello from Store.Index()" End Function
    FakePre-694acc221b584323a32881ea0d52035a-7d4bfe68683e4732b3d65719eec17438
    

  2. Add the Browse and Details methods. To do this, add the following code to the StoreController:

    (Code Snippet – ASP.NET MVC 3.0 Fundamentals – Ex2 StoreController BrowseAndDetails – CSharp)

    C#

    // // GET: /Store/Browse public string Browse() { return "Hello from Store.Browse()"; } // // GET: /Store/Details public string Details() { return "Hello from Store.Details()"; }
    FakePre-177ef07c23364110b1e5f82696e4a326-19459c24c6d940d390ec8662780ca1efFakePre-0a5d3fc2b6bb490e8dd24426e5153155-33e8c6e91e494271a6aba1c6910f4bbf
    

    (Code Snippet – ASP.NET MVC 3.0 Fundamentals – Ex2 StoreController BrowseAndDetails – VB)

    Visual Basic

    ' 'GET: /Store/Browse Public Function Browse() As String Return "Hello from Store.Browse()" End Function ' 'GET: /Store/Details Public Function Details() As String Return "Hello from Store.Details()" End Function
    FakePre-af9fa93ececd4f24937a97771f7670f4-e15fe743e3e7423eae08fd04de817515FakePre-9176e80a27b14637ba4e8082fc76dd3a-63df0bfe32f84b979e53d27c3cc22232FakePre-b305eed745b44d4bb14c21db0c1546a3-ede968d42be94ff3a10f91743f3cccabFakePre-c2b285aad9c648358c4d70ab63067915-e4d6b479a9744c06833c20ac2e4606a3FakePre-a07b180015ac44f2b96237e97394d55a-57649814abe8491eb9fa5a89e14a33b4FakePre-a85605876d1143f48b37716e9abfd526-4f402a5bf3d14f768fe897a655715b1b
    

Task 3 – Running the Application

In this task, you will try out the Application in a web browser.

  1. Press F5 to run the Application.
  2. The project starts in the Home page. Change the URL to verify each action’s implementation.
    1. /Store. You will see “Hello from Store.Index()”.
    2. /Store/Browse. You will see "Hello from Store.Browse()".
    3. /Store/Details. You will see “Hello from Store.Details()”.

      Figure 20

      Browsing /Store/Browse

  3. Close the browser.

Next Step

Exercise 3: Passing parameters to a Controller