3.1Controller role
The Controller in MVC mode is mainly used to respond to user input and modify the corresponding model (Module ). It focuses on the application stream, processes the input data, and outputs the corresponding View ). The URL tells the router which controller to instantiate, which method to call, and provides the required parameters for the method. Then, the Controller method determines the view and then renders the view.
The URL does not directly correspond to the files stored on the WEB server disk, but is related to a controller method. ASP. net mvc implements the transformation of the front-end controller in MVC mode. The routing subsystem is at the beginning, followed by the Controller.
3.2Controller Basics
3.2.1Simple Example:Home Controller
The project created using the Internet Application template contains two controller classes:
1. HomeController ---- Home Page and About Page under the root directory of the website
2. AccountController-handles account requests
In the VS project, expand the/Controller folder and open the HomeController. cs file as follows:
It is a simple class that inherits the Controller base class. The Index method is used to process the access to the home page and call the Index view for output.
Experience summary:
1. You do not need to perform any additional configuration. You can view/Home/Index to execute the Index method in the IndexController class. This is the route in the operation.
2. The Controller class inherits System. Web. Mvc. Controller.
3. The controller is the core of MVC. Any user input must be processed by the controller. The Controller determines which method to call, and the returned view or String
3.2.2Parameters in controller operation
The method parameters in the controller can be passed through the following two methods:
1. Pass parameters through the query string in the URL, for example:
// Get:/Store/Browse? Genre = Disco
Public string Browser (string genre)
{
Return genre;
}
2. Embed parameters into URLs for transmission, for example:
// GET:/Store/Details/5
Public string Detail (int id)
{
Return id. toString ();
}
Implementation principle of the preceding two parameters: the router maps the URL into the corresponding operation, including the transmitted parameters, the called controller, method, and view output.
Summary:
The Controller is the controller of the MVC application. It carefully orchestrates the interaction between users, module objects, and views. It also responds to user input, calls the correct module, and outputs an appropriate view to respond to user requests.