ASP. net mvc 5 getting started tutorial (2) Controller, mvccontroller
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) creating a project
Download source code: Click here to download
As the name suggests, an MVC project consists of three parts: M-Model, V-View, and C-Controller. The diagram is as follows.
We can see that the request initiated by the user will first arrive at the Controller.
Controller definition: A controller is a type that can process requests sent from a browser, obtain data from the Model, and then pass the processed data through the View) send to the browser.
First, create a Controller. In Solution Explorer on the Right of VS, right-click Controllers, select add, and then click controller. As shown in. The Controllers folder is used to store all Controllers. This is not mandatory, but it is a conventional habit.
In the displayed window, select MVC 5 controller-null. Click OK. Then enter the Controller name FirstController and click OK.
VS will create the 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{ public class FirstController : Controller { // // GET: /First/ public string Index() { return "
The FirstController here is a controller. The method in the controller is called Action. The controller here contains an Action called Index. This Action can send a string to the browser, which will be displayed in the browser.
Compile and run the entire project. The browser window displays the home page, as shown in.
Add First/to the url of the home page and press enter to request the page. The returned string of the Index Action of the FirstController is displayed. As shown in.
Then add index/to the end of the path and press enter to request the page. It is still the string returned by the Index Action of the FirstController controller. As shown in.
So the question is, how does one define access to the Controller and Its actions through URLs?
This is defined by routing. Routing will be discussed in the next section.