ASP. NET mvc4.0 Getting Started Guide-Add a controller

Source: Internet
Author: User

MVC Concept

The meaning of MVC is "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.

Add Controller

Let's start creating a controller class.

In Solution Explorer, right-click "controllers" and choose "New"> "controller ......", Enter the Controller name "helloworldcontroller" and keep the default options for the template ("Empty MVC controller"). Click "add" to add the Controller class.CodeAs follows:

 Using  System; Using  System. Collections. Generic;  Using  System. LINQ;  Using  System. Web;  Using  System. Web. MVC;  Namespace  Mvcmovie. controllers {  Public   Class Helloworldcontroller: controller {  //          //  Get:/helloworld/          Public  Actionresult index (){  Return  View ();}}} 

The modification code is as follows:

 Namespace  Mvcmovie. controllers {  Public  Class  Helloworldcontroller: controller {  Public   String  Index (){  Return   "  This is my <B> default </B> method.  "  ;}  Public   String Welcome (){  Return   "  This is a welcome method.  "  ;}}} 

In this example, the Controller method returns the HTML of a string. This controller is named helloworldcontroller, and the first method in the Code is named index. 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, http: // localhost: 5279/helloworld in the following example ). 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 first part of the URL determines that the controller class will be executed. Therefore/HelloworldMaps to the helloworldcontroller controller class. The second part of the URL 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 Controller class calls the index method by default.

BrowseHTTP:// Localhost: xxxx/helloworld/welcome. The welcome method is run and returns a string: "This is the welcome 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 = Tom & num = 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 num parameter is not passed, the default value is 1.

   Public   String Welcome ( String Name,Int Num = 1  ){  Return   "  Hello,  " + Name + "  ,  " + Num + "  Times "  ;} 

Run your application and browse this URL (Http: // localhost: xxxx/helloworld/welcome? Name = Tom & num = 4), The result is "Hello, Tom, four times". You can try different values for the parameter name and num. The ASP. net mvc model binding mechanism automatically maps query strings in URLs in the address bar to parameters in your method.

Note: readers who are good at thinking may make the following attempts.

 Public StringWelcome (StringName ){Return "Hello,"+Name ;}

Enter:Http: // localhost: 5279/helloworld/welcome/TomThe output is "hello,", and the expected "Hello, Tom" is not output ".

Isn't it/[controller]/[actionname]/[parameters? Why cannot I obtain the parameters passed in the URL? This is related to the routing conventions. By default, the parameter identifier is ID to pass in correctly, as shown below:

Public StringWelcome (IntID ){Return "ID:"+ID ;}

This question is mentioned here. Readers do not need to go into it. In the future, there will naturally be some places to introduce its principles.

In these two examples, the Controller has been working on the "VC" part of MVC, that is, the view and controller. 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.

 

AllArticleNavigation

This series contains 10 articles 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.

The original article is for 9 articles, and the translator splits 6th of them into 2

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/seawaving/archive/2012/12/03/2800210.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/seawaving/archive/2012/12/04/2801949.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/seawaving/archive/2012/12/04/2801988.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/seawaving/archive/2012/12/05/2803012.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/seawaving/archive/2012/12/05/2803429.html

6. view the Edit Method and edit 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/seawaving/archive/2012/12/05/2804100.html

Http://www.cnblogs.com/seawaving/archive/2012/12/06/2804590.html

7. Add fields for the movie model and database table

· 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

· Address: http://www.cnblogs.com/seawaving/archive/2012/12/06/2805401.html

8. Add verification for the 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/seawaving/archive/2012/12/06/2806322.html

9. view the detail and delete Methods

· 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/seawaving/archive/2012/12/10/2811064.html

 

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.