MVC Learning Notes--controller

Source: Internet
Author: User

First, create a new controller

Action : Select the Controllers folder, right-Add/controller, and give the name Eg:storecontroller

determine If a class is a controller class: Whether the class inherits from the Using System.Web.Mvc.Controller

Clear : Models and views are useful, but the controller is the real core, and each request must be handled by the controller, which is the "Commander" of the MVC application!

Code Demo :

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSYSTEM.WEB.MVC;namespacewebapplication1.controllers{ Public classStorecontroller:controller {//Get:store         Public stringIndex () {//return View ();            return "Hello from Store.index ()"; }         Public stringBrowse (stringgenre) {            stringMessage = Httputility.htmlencode ("store.browse,genre="+genre); returnmessage; }         Public stringDetails () {return "Hello from Store.details ()"; }    }}
View Code

Run the project, and then browse to the following URL

/store

/store/browse

/store/details

Accessing these URLs invokes the action method in the controller and returns a response string after meals, as follows:

Special case of parameters in controller operation:

public string Example (int ID)
{
String message = "Hello from store.example,num=" + ID;
return message;
}

When the parameter name in the above method is ID, and the data type is int, the default routing convention for ASP. NET MVC can pass parameters by/STORE/EXAMPLE/5, and of course it can http://localhost:2261/Store/ example?id=5, note: The parameter name must be an ID, and an int

MVC Learning Notes--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.