ASP. net mvc 5 getting started tutorial (2) Controller, mvccontroller

Source: Internet
Author: User

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.

Related Article

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.