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