ASP. MVC4 Getting Started Guide (2): Adding a Controller

Source: Internet
Author: User

MVC represents: model - view - controller . MVC is a well-architected and easy-to-test and maintainable development model. Applications based on the MVC pattern include:

· Models: Data class that represents the data for the application and uses validation logic to enforce the business rules.

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

· Controllers: Process the browser request, obtain the data model, and then specify the view template to respond to the browser request.

In this series of tutorials, we'll cover all of these concepts and show you how to use them to build your application.

First, let's create a controller class. In Solution Explorer , right-click the Controller folder, and then select Add Controller.

Name the new controller as "Helloworldcontroller". Leave the default template as "Empty MVC Controller" and click "Add".

Note that a new file named HelloWorldController.cs is created in Solution Explorer . The file will be opened by default by the IDE.

Replace the contents of the file with the following code.

usingSystem.Web;usingSystem.Web.Mvc;  namespaceMvcMovie.Controllers {     publicclassHelloWorldController : Controller     {         //         // GET: /HelloWorld/          publicstringIndex()         {             return"This is my <b>default</b> action...";         }          //         // GET: /HelloWorld/Welcome/           publicstringWelcome()         {             return"This is the Welcome action method...";         }     } }

In this example, the Controller method returns the HTML of a string. The first method in the named Helloworldcontroller code of this controller 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, in the following example: Http://localhost:1234/HelloWorld) the page behaves as follows in the browser. In the method above, the code returns a string directly. You tell the system to return only some HTML, and the system does!

Different controller classes (and their different methods of operation) are called according to the incoming url,asp.net MVC. Use the default URL routing logic format for ASP. NET MVC to determine which code is called:

/[controller]/[actionname]/[parameters]

The first part of the URL determines that the controller class will be executed. So /helloworld maps to the Helloworldcontroller controller class. The second part of the URL determines which operation method to execute in the controller class. Therefore, /helloworld/index will cause HelloWorldController the Controller class's index method to be executed. Note that we only need to browse the /helloworld path and the index method is called by default. If no explicit action method is specified, the index method is called by the Controller class by default.

Browse http://localhost:xxxx/helloworld/welcome. The Welcome method is run and returns the string: "This is the Welcome action method ...". The default MVC map is/[controller]/[actionname]/[parameters] for this URL, the controller class is HelloWorld and the action method is welcome, and you have not used the [Parameters] part of the URL.

Let's change this example a little bit so that you can use the URL to pass some parameter information to the controller class (for example, /helloworld/welcome?name=scott&numtimes=4). Change your welcome method to include two parameters, as shown below. It is important to note that the sample code uses the optional parameters feature of the C # language, and the Numtimes parameter has a default value of 1 when the value is not passed.

publi C  string  welcome ( string  name, int  numtimes = 1) {        return   Httputility.htmlencode ( "Hello"  + name + "Numtimes is:"  + numtimes);

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

In these two examples, the controller has been doing the "VC" part of MVC function. That is, the work of the view and the controller. The controller returns HTML content directly. Typically, you will not let the controller return HTML directly, because the code becomes cumbersome. Instead, we typically use a separate view template file to help generate the returned HTML. Let's take a look at how we can do that.

ASP. MVC4 Getting Started Guide (2): Adding a Controller

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.