ASP. NET MVC 5 Getting Started Tutorial (2) Controller controllers

Source: Internet
Author: User

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

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.