ASP. NET MVC3-section 02nd-Add a Controller (C #)

Source: Internet
Author: User

Preface -------------------------- section 01st [translation] 01-ASP.NET MVC 3 Introduction ---------------------------- MVC is the abbreviation of "model-view-controller", that is, model-view-controller, MVC is an architecture that separates concerns. This will make it easy to develop and maintain basic MVC programs, including: Controllers: A controller that gets parameters from a program and receives data, and do special processing, create a view, and then respond to the class model (Models) of the Client: is a reference to reflect (represent) data, and makes the data correct, views: a dynamically generated HTML response template file used by your program, it is a template file used to generate HTML. We will use these instructions to show you how to use them to create an MVC program. We will create a controller class and Solution Explorer in the Solution, right-click the "Controllers" folder and choose Add -- Controller Add controller to name your new Controller "HelloWorldController". Select the Empty controller Empty Controller in the template and click Add) note that a new file is created in the solution: HelloWorldController. cs, the file is opened in the IDE's public class HelloWorldController block, create two methods following the code --------- this controller will return the HTML character code as a column to copy the code 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... ";}} copy the code. Your controller is named HelloWorldController. The first method is named" Index ". Run it in the browser (Press F5 or Ctrl + F5 )., "HelloWorld" appears in the address bar of the browser. http://localhost:43246/HelloWorld This page looks like this. We can see that some HTML tags can also be rendered by browsers <B> default </B> ASP. different controller classes created by. net mvc, which have different action methods. The requested action is matched by different routing rules based on different URLs, the routing rules are as follows:/[Controller]/[ActionName]/[Parameters] the first part of the URL indicates the Controller class to be executed, therefore,/HelloWorld corresponds to the HelloWorldController class URL. The second part is the class execution method, therefore, the/HelloWorld/Index corresponds to the Index method in HelloWorldController. If you access it through a browser" http://localhost:xxxx/HelloWorld/Welcome . "The Welcome method will be called and return This is the Welcome action method... the default MVC routing rule is/[Controller]/[ActionName]/[Parameters]. the third part of the URL is [Parameters]. This is the example of passing a parameter in the network controller method, this allows us to pass some parameters such as/HelloWorld/Welcome to the Controller through URL? Name = Scott & numtimes = 4 change your Welcome method to add two parameters. The following Code indicates that the Code uses the C # attribute parameter and the numTimes parameter defaults to 1, public string Welcome (string name, int numTimes = 1) {return HttpUtility. htmlEncode ("Hello" + name + ", NumTimes is:" + numTimes);} run the program and access address http://localhost:xxxx/HelloWorld/Welcome ? Name = Scott & numtimes = 4 you can see different names and time names and numtimes the routing system will automatically match the parameters requested in the address bar to your Method

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.