[Official tutorial] Asp. Net MVC4 Getting Started Guide (2): Add a controller and mvc4 Getting Started Guide

Source: Internet
Author: User

[Official tutorial] Asp. Net MVC4 Getting Started Guide (2): Add a controller and mvc4 Getting Started Guide

2. Add a controller

· Original address: http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-controller

· Address: http://www.cnblogs.com/powertoolsteam/archive/2012/11/02/2751015.html


Asp. Net MVC4 Getting Started Guide (2): Add a controller

MVC representatives:Model-View-Controller. MVC is a well-structured and easy-to-test and easy-to-maintain development model. MVC-based applications include:

·MOdels: indicates the data class of the application and uses the verification logic to force the implementation of business rules.

·VIews: The template file used by the application to dynamically generate HTML.

·COntrollers: Process browser requests, obtain the data model, and specify the view template to respond to browser requests.

This series of tutorials will cover all these concepts and show you how to use them to build applications.

First, let's create a controller class. InSolution Resource Manager, Right-click the Controller folder, and select"Add controller".

Name the new controller as "HelloWorldController ". Retain the default template as "Empty MVC controller" and click "add".

Note thatSolution Resource ManagerCreates a new file named HelloWorldController. cs. This file will be opened by IDE by default.

Use the following code to replace the content in the file.

using System.Web;using System.Web.Mvc;  namespace MvcMovie.Controllers{    public class HelloWorldController : Controller    {        //        // GET: /HelloWorld/          public string Index()        {            return "This is my <b>default</b> action...";        }          //        // GET: /HelloWorld/Welcome/          public string Welcome()        {            return "This is the Welcome action method...";        }    }}

In this example, the Controller method returns the HTML of a string. This controller is named Index as the first method in the HelloWorldController code. Let's call it from the browser. Run the application (Press F5 or CTRL + F5 ). Enter the path "HelloWorld" in the address bar of the browser. (For example, in the following example: http: // localhost: 1234/HelloWorld) the page displays the following in the browser. In the above method, the Code directly returns a string. You told the system to return only some HTML. The system did this!

According to the input URL, ASP. net mvc calls different controller classes (and different operation methods among them ). Use the default URL routing logic format of ASP. net mvc to determine which code will be called:


The URL in the first part determines that the controller class will be executed. Therefore/HelloWorldMaps to the HelloWorldController controller class. The URL in the second part determines the operation method in the Controller class to be executed. Therefore/HelloWorld/Index,Will makeHelloWorldControllerThe Index method of the controller class is executed. Please note that we only need to browse/HelloWorldPath. By default, the Index method is called. If no specific operation method is specified, the Index method is called by the Controller class by default.

BrowseHttp:// Localhost: xxxx/HelloWorld/Welcome. The Welcome method is run and returns the string "This is the Welcome action method ...". The default MVC ing is/[Controller]/[ActionName]/[Parameters]. For this URL, the Controller class is HelloWorld and the operation method is Welcome, you have not used the [Parameters] section of the URL.

Let's slightly modify this example so that some parameter information can be transmitted to the Controller class using URL (for example,/HelloWorld/Welcome? Name = Scott & numtimes = 4). Change your Welcome method to include two parameters, as shown below. Note that the sample code uses the optional Parameter Function of the C # language. When the numTimes parameter is not passed, the default value is 1.

public string Welcome(string name,int numTimes = 1) {     return HttpUtility.HtmlEncode("Hello " + name +", NumTimes is: " + numTimes);}

Run your application and browse this URL (Http: // localhost: xxxx/HelloWorld/Welcome? Name = Scott & numtimes = 4). You can try different values for the parameter name and numtimes. ASP. net mvc model binding system automatically maps the query string in the URL in the address bar to parameters in your method.

In these two examples, the Controller has been working on the "VC" part of MVC. That is, the work of views and controllers. The Controller directly returns HTML content. Generally, you do not want the Controller to directly return HTML because the code becomes very cumbersome. Instead, we usually use a separate view template file to help generate the returned HTML. Let's take a look at how we can achieve this.

Controller is an important part of MVC. With the knowledge in this section, I believe you will have a better understanding of MVC. What tools can be used in addition to the above knowledge during MVC development? ComponentOne Studio ASP. net mvc is a control package for the MVC platform. It is seamlessly integrated with Visual Studio and fully compatible with MVC6 and ASP. NET 5.0, which greatly improves work efficiency.

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.