ASP. NET mvc4 Getting Started Guide (2): Add a controller

Source: Internet
Author: User

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

·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 followingCodeReplace 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:

/[Controller]/[actionname]/[parameters]

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.

 

Download the complete document:ASP. NET mvc41_1_timeout

Certificate -----------------------------------------------------------------------------------------------------------------------------------------------

Note:

9 articles in this seriesArticle, Translated from ASP. net mvc4 official tutorial, due to the concise description of this series of articles, the length is moderate, from an example to explain, the full text finally completed a small system for managing movies, very suitable for beginners ASP.. Net mvc4. Nine articles:

1. Introduction to ASP. NET mvc4

· Original address: http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/intro-to-aspnet-mvc-4

· Address: http://www.cnblogs.com/powertoolsteam/archive/2012/11/01/2749906.html

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

3. Add a view

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

· Address: http://www.cnblogs.com/powertoolsteam/archive/2012/11/06/2756711.html

4. Add a model

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

· Address: http://www.cnblogs.com/powertoolsteam/archive/2012/12/17/2821495.html

5. Access the data model from the Controller

· Original address: http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/accessing-your-models-data-from-a-controller

· Address: http://www.cnblogs.com/powertoolsteam/archive/2013/01/11/2855935.html

6. Verify the editing method and view

· Original address: http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/examining-the-edit-methods-and-edit-view

· Address: http://www.cnblogs.com/powertoolsteam/archive/2013/01/24/2874622.html

7. Add new fields to the movie table and Model

· Original address: http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-new-field-to-the-movie-model-and-table

· Translation address:Http://www.cnblogs.com/powertoolsteam/archive/2013/02/26/2933105.html

8. Add a validator to the Data Model

· Original address: http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-validation-to-the-model

· Address: http://www.cnblogs.com/powertoolsteam/archive/2013/03/05/2944030.html

9. query details and delete records

· Original address: http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/examining-the-details-and-delete-methods

· Address: http://www.cnblogs.com/powertoolsteam/archive/2013/03/07/2948000.html

10. Third-party control studio for ASP. NET wijmo mvc4 tool Application

Address: http://www.cnblogs.com/powertoolsteam/archive/2013/05/09/3068699.html

 

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.