asp.net MVC4 Introductory Tutorial (ii): Add a controller _ self-study process

Source: Internet
Author: User

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

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

· Views: The template file that the application uses to dynamically generate HTML.

· Controllers: handles the browser's request, obtains the data model, and then specifies the view template to respond to the browser request.

In this series of tutorials, we'll cover all of these concepts and tell 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 "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.

Using System.Web;
Using SYSTEM.WEB.MVC; 
 
Namespace Mvcmovie.controllers 
{public 
 class Helloworldcontroller:controller 
 {////Get 
  :/ helloworld/public 
 
  String Index () {return ' This are my 
   <b>default</b> action ... '; 
  } 
  //Get:/helloworld/welcome/public 
 
  string Welcome () 
  {return ' This is 
   ' Welcome action method ... ."; 
  } 
 } 
}

In this example the Controller method will return the HTML of a string. This controller is named the first method in the Helloworldcontroller code 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 like the screenshot below in the browser. In the above method, the code returns a string directly. You tell the system to return only some HTML, and the system does!

Call different controller classes (and the different methods of operation) based on the incoming url,asp.net MVC. Use the default URL routing logic format of asp.net mvc to determine which code is invoked:

Copy Code code as follows:
/[controller]/[actionname]/[parameters]

The first part of the URL determines which controller class will be executed. Therefore /helloworld is mapped to the Helloworldcontroller controller class. The second part of the URL determines which action method to perform in the Controller class. so /helloworld/index will make HelloWorldController the index method of the controller class executed. Note that we only need to browse the /helloworld path and call the index method 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 runs and returns a string: "This is the Welcome action methods ...". The default MVC map is/[controller]/[actionname]/[parameters] for this URL, the controller class is HelloWorld, the action method is welcome, and you have not used the [Parameters] part of the URL.

Let's modify 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 should be noted that the sample code uses the optional parameter feature of the C # language, and the default value of the Numtimes parameter is 1 when the value is not passed.

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 the parameters in your method.

In these two examples, the controller has been doing the "VC" part of the MVC function. That is, the work of views and controllers. The controller returns HTML content directly. Typically, you don't 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 see how we can do that here.

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.