Controllers (Controller) –asp.net MVC 4 series

Source: Internet
Author: User

Create an ASP. NET MVC 4 WEB Application project and name the program Mvcmusicstore, such as:

Controller

In the MVC pattern, the controller is primarily responsible for responding to user input, modifying the model in response, and providing output data for the associated view.

The requested URL is first intercepted by the routing mechanism to determine which controller to instantiate, which operation method to invoke, and does not provide the required parameters for the method. The controller's method then determines which view to use and renders the view .

Add a new Empty controller named Storecontroller:

Create a Browse, Details action method, and change the return value of 3 methods from ActionResult to String and run the View effect:

 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 ()";
    }
}

Although models and views are useful in ASP. NET MVC, the controller is the real core. Each request must be handled by the controller, but some of these requests do not require models and views .

Modify the Browse method as follows:

GET:/store/browse?genre=? Disco
 Public string Browse (string genre)
{
    string message = Httputility.htmlencode ("store.browse,genre =" + Genre);
    return message;
}

Httputility.htmlencode () to preprocess user input, which prevents users from injecting JavaScript code or HTML markup into the view with links , such as/store/browse? genre=<script>window.location= ' http://hacker.example.com ' </script>

Modify the Details method so that it reads and displays an input parameter named ID, where the ID value is embedded in the URL:

GET:/STORE/DETAILS/5
 Public string Details (int ID)
{
    string "store.details, id =" + ID;
    return message;
}

The controller operation feels like a method in the Controller class that the Web browser calls directly. Classes, methods, and parameters are materialized into specific path fragments or query strings in the URL.

Controllers (Controller) –asp.net MVC 4 series

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.