ASP. net mvc music store tutorial (2): Controller

Source: Internet
Author: User

From http://firechun.blog.163.com/blog/static/3180452220110272197830/

In the traditional Web architecture, URLs are always mapped to files on disks. For example, a URL similar to "/products. aspx" or "/products. php" may be processed by the "products. aspx" or "products. php" file.

In the MVC-based Web architecture, URL ing is somewhat different. The ing to a file is replaced with the method mapped to the class. These classes are called "controllers" (controllers ), they are used to respond to and process HTTP requests, user input, receive and save data, and decide how to send the response back to the client (display HTML, download files, redirect URLs, etc ).

Add homecontroller
We can add a controller class to start our MVC Music Store application.Program. According to the naming conventions of ASP. net mvc, we name this class "homecontroller ".

In Solution Explorer, right-click the "controllers" folder and choose "Add-> controller:

In the "add controller" dialog box, change the default name to "homecontroller" and press "add.

This will create a new file-homecontroller. CS, which contains the followingCode:

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;
Using system. Web. MVC;

Namespace mvcmusicstore. Controllers
{
Public class homecontroller: Controller
{
//
// Get:/home/
Public actionresult index ()
{
Return view ();
}
}
}

To make it as easy as possible, we can replace the original index method with a simple string return method. We only need to make two simple modifications.

    • Change the return type of the method from actionresult to string.
    • Modify the Return Statement and return the string "hello from home"
 
Now, the method looks like this:
Public String index () {return "hello from home ";}
 
Start the application
 
The original is very detailed, never use vs to see the original also know how to run, here do not write, in short now can start the program, press F5 (debug startup) or Ctrl-F5 (do not debug startup)
 
We can see this:
 
OK, which is quite quick. We created a new web site and added three lines of code to present a piece of text in the browser. Obviously, this is just the beginning.
 
(The description text about vs virtual IIS server is not written)
 
Add storecontroller
 
We have added a simple homecontroller to implement the home page of the website. Now, we have added another controller to implement the browsing function of the music repository. The repository controller implements the following functions:
    • Music repository category list page
    • List all music records in a category
    • Displays the details of a specified music record.
 
(The original article is too long to be written)
 
Repeat the process of adding homecontroller and change the name to storecontroller.
 
The new storecontroller already has the index method. We use the index method to implement the list page of all categories, and add two methods browse and details to implement the other two functions.
The methods in these controllers-index, browser, and detail are called "Controller actions", as you can see in homecontroller. the index () Action Method responds to the URL request and (usually) determines what content is sent back to the browser or the URL requested by the user.
 
We modify the index () method to let it return the string "hello from store. index () ", the Browse () and details () methods are also modified to complete the storecontroller implementation.
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;
Using system. Web. MVC;

Namespace mvcmusicstore. Controllers
{
Public class storecontroller: Controller
{
//
// Get:/store/
Public String index ()
{
Return "hello from store. Index ()";
}
//
// Get:/store/browse
Public String browse ()
{
Return "hello from store. Browse ()";
}
//
// Get:/store/details
Public String details ()
{
Return "hello from store. Details ()";
}
}
}
 
Start the project again and browse the following URLs respectively:
    • /Store
    • /Store/browse
    • /Store/details
 
Accessing these URLs will call the action methods in the Controller and return a string:
 
 
 
Great! But this is just some fixed strings. Let's make them dynamic: Get some information from the URL and output it to the webpage.
 
First, we modify the browser action method so that it receives the parameter (querystring) from the URL ). We add a parameter -- gener for the action method. When the action method is called, Asp. net MVC will automatically pass a parameter named "gener" for the URL's querystring or form submission.
//
// Get:/store/browse? Genre =? Disco
Public String browse (string genre)
{
String message = httputility. htmlencode ("store. Browse, genre ="
+ Genre );

Return message;
}
 
Note: We use the httputility. htmlencode method to review user input, which can prevent users from using similar/store/browse? Genre = <SCRIPT> window. Location = 'HTTP: // hackersite.com '</SCRIPT>
 
Now, let's Browse/store/browse? Genre = disco
 
 
 
The next modification is to let the details action read and display an input parameter named "ID. Unlike the previous method, we do not want to put the id value in the querystring parameter, but directly use it as a part of the URL, for example:/store/details/5
 
ASP. net MVC makes it easy for us to implement this idea without any settings, Asp. net MVC default routing settings regard the part after the action method in the URL as a parameter named "ID". If your action method has a parameter named "ID", Asp. net MVC will automatically pass this part as a parameter.
//
// Get:/store/details/5
Public String details (int id)
{
String message = "store. Details, id =" + ID;

Return message;
}
 
Run the application and browse/store/details/5
 
 
 
See what we have done so far:
    • Create a new ASP. net mvc project in
    • The basic directory structure of ASP. net mvc applications is discussed.
    • I learned how to use ASP. NET development server to run web sites (I didn't write this part ...)
    • Two Controller classes are created: homecontroller and storecontroller.
    • Added an Action Method to the Controller to respond to the URL request and return the text to the browser.

 

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.