Original: ASP. NET MVC 5 Getting Started Tutorial (2) Controller controllers
Article Source: slark.net-Blog Park http://www.cnblogs.com/slark/p/mvc-5-get-started-controller.html
Previous section: ASP. NET MVC 5 Getting Started Tutorial (1) New project
Next section: ASP. NET MVC 5 Getting Started Tutorial (3) Routing route
SOURCE download: Click I download
As the name implies, an MVC project consists of three parts: the m-model-model, the v-view-view, and the c-controller-controller. The diagram is shown below.
Aside from the overall operating mechanism of MVC, we can see that the user-initiated request first arrives at the director Controller.
Controller definition: A controller is a class that handles requests from the browser, fetches the data from the model, and then sends the processed data to the browser through the view.
Let's start by creating a controller. Right-click Controllers in the Solution Explorer on the VS right side and select Add, then click Controller. As shown in. The Controllers folder is used to store all controllers. This is not a mandatory rule, but it is a customary practice.
In the pop-up window, select MVC 5 controller-NULL. Click OK. After entering the controller name Firstcontroller, then click OK.
VS creates a FirstController.cs file in the Controller folder, which is the first controller we created. As shown in.
Write the following code to the FirstController.cs file
using SYSTEM.WEB.MVC; namespace slarkinc.controllers{ publicclass firstcontroller:controller { // // GET:/first/ Public string Index () { return""; } }}
Here the Firstcontroller is a controller, the controller in the method called action, where the controller contains an action called Index. This action can send a string to the browser, and the browser displays the string.
Compile to run the entire project. The browser window displays the home page, such as.
Add first/after the URL of the home page, and then press ENTER to request the page. You will see the string returned by the index action of the Firstcontroller controller. As shown in.
Then add index/after this path and press ENTER to request the page. is still the string returned by the index action of the Firstcontroller controller. As shown in.
So the question is, how does the URL to access the controller and its action define?
This is defined by road origin. The route will be described in the next section, so please look forward to it.
ASP. NET MVC 5 Getting Started Tutorial (2) Controller controllers