"Translation" MVC Music Store Tutorial-Overview (iii)

Source: Internet
Author: User

Controller

With a traditional web framework, the incoming URLs are typically mapped to files on disk. For example: a URL request "/products.aspx" or "/products.php" is a file that processes a products.aspx "or" products.php "

The MVC framework's incoming URLs are different from mapping to server code, rather than passing URLs to files, but rather mapping to method methods or classes, which are called "controllers" who handle incoming HTTP requests, process user input, retrieve and save data, and decide to return the data to the client (display URL, download file, jump to a different URL, etc.).

Add HomeController

We'll add a controller to the MVC Music Strore project to handle the site's home page request, and we'll follow the default naming rules for ASP. NET MVC, named HomeController.

Right-click the Controller folder in the Solution Explorer, select "Add" and select "Controller"

In the "Add Controller" dialog box that pops up, name the Controller "HomeController" and click the "Add" button

A new file is created and the HomeController.cs code is as follows:

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingsystem.web;5 usingSYSTEM.WEB.MVC;6 7 namespacemvcmusicstore.controllers8 {9      Public classHomecontroller:controllerTen     { One         // A         //GET:/home/ -  -          Publicactionresult Index () the         { -             returnView (); -         } -  +     } -}
View Code

To make it as simple as possible, we let the index method simply return a string, and we need to make two changes:

    • Modify the return type of the method to string instead of ActionResult
    • Modify the return value statement to "Hello from Home"

Method is modified to "

1  Public string Index () 2 {3return"Hello from Home"; 4 }
View Code

Running the application

Now that we run the application, we can start the Web service by either of the following methods:

    • Select debug-> Start Debugging Menu
    • Click the Green Run button on the tool bar

    • Use the shortcut keys on your keyboard. F5

Using either of these methods, our project will be compiled to launch the built-in ASP. At the bottom of the screen you will be notified that the ASP. NET Server is started and displays the port number the application is running on.

Visual WEB Developer will automatically open a browser and point to our request address. This will allow us to quickly use my program:

OK, very simple-we created a new website, added three lines of functions, displayed some text on the browser, everything was simple, but it was a start.

Add a Storecontroller

We added a simple homecontroller to implement our home page feature, next we add another controller is to implement the music store browsing function, Stroe controller will support three scenarios:

    • List all the music styles in the store
    • Album browsing page, listing all albums under a genre
    • Display detail information for an album

We start by adding a new Storecontroller, if you are not ready to close the program, you can either close the browser directly or choose the Stop program from the "Debug->stop Debugging" menu.

Add Storecontroller like add HomeController, right-click the Controller folder in Solution Explorer, select "Add", select "Controller"

Stroecontroller already has the "index" method, we will use the "index" method to implement all the categories in the Music store list, we will add an additional two methods to achieve "browsing" and "detail".

These methods (Index,browse and details) are called "Controller actions" inside the controllers, and you have seen the Index () method in HomeController, which is the job of requesting the URL, Determine what content needs to be returned to the browser or.

We will begin to return the string "Hello from Stroe" in Stroecontroller's Index (). Index (), and then add a similar method to browse (), Details ().

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingsystem.web;5 usingSYSTEM.WEB.MVC;6 7 namespacemvcmusinstore.controllers8 {9      Public classStorecontroller:controllerTen     { One         // A         //GET:/store/ -  -          Public stringIndex () the         { -             return "Hello from Store.index ()"; -         } -  +          Public stringBrowse () -         { +             return "Hello from Store.browse ()"; A         } at  -          Public stringDetails () -         { -             return "Hello from Store.details ()"; -         } -  in     } -}
View Code

Run the project again to access the following address

    • /store
    • /store/browse
    • /store/details

These methods will return the string we returned in the action

Great, but these are just fixed strings, we make him a little bit more dynamic, get some additional information from the URL, and then display them on the page.

First, we get the parameter value from the URL in the Browse action, and we can add a "genre" parameter to the action, and ASP. NET MVC automatically passes the value assigned to it named genre.

1 //2 //GET:/store/browse?genre=disco3  Public stringBrowse (stringgenre)4 {5 stringMessage = Httputility.htmlencode ("store.browse, Genre ="+genre);6 returnmessage;7}
View Code

Now we browse this address:/store/browse? Genre=disco

Next, we modify the details Action to read and display an input parameter named ID, different from the previous method, this time we are not passing this parameter in the request parameter, but in the requested URL address. For example:/STORE/DETAILS/5.

in ASP. NET MVC, we don't need to configure anything, and the ASP. ASP. If your action method has a parameter named ID, the net MVC default route takes the part after the action as the parameter value named ID. NeT MVC will automatically pass this part as a parameter to the Action method.

1 //2 //GET:/STORE/DETAILS/53  Public stringDetails (intID)4 {5 stringMessage ="store.details, ID ="+ID;6 returnmessage;7}
View Code


Now we browse this address:/STORE/DETAILS/5:

Let's make a summary.

    • An ASP. NET MVC Project was created
    • The basic file folders of the project are discussed, as well as the role
    • Learned how to run a development server
    • Created two controllers HomeController and Storecontroller
    • Added the Action method to the controller.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.