Walkthrough 2-3: simple controller exercises

Source: Internet
Author: User

Source: http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-controller

1. Create a default ASP. net mvc 4 Project

2. Add a helloworld controller and select "Empty MVC controller"

3. Write the helloworld controller Code as follows.

 1     public class HelloWorldController : Controller  2     {  3         //  4         // GET: /HelloWorld/  5   6         public string Index()  7         {  8             return "This is my <b>default</b> action...";  9         } 10  11         // 12         // GET: /HelloWorld/Welcome/ 13  14         public string Welcome() 15         { 16             return "This is the Welcome action method..."; 17         } 18     } 

The return type of the index () method is string rather than actionresult.

The default URL routing format is/[controller]/[actionname]/[parameters]. Therefore, "/helloworld/Index" corresponds to it and calls the index method of the helloworld controller. "/Helloworld" has the same effect, because the default actionname is "Index ".

4. Run the program

5. modify the code to understand the parameter parameters.

Modify the code of the welcome () method as follows.

1 public string Welcome(string name, int numTimes = 1) {2      return HttpUtility.HtmlEncode("Hello " + name + ", NumTimes is: " + numTimes);3 }

Name and numtimes are used to receive the values in the route. numtimes is 1 by default. Run the program. Enter the address"Http: // localhost: xxxx/helloworld/welcome? Name = Scott & numtimes = 4 ".

Walkthrough 2-3: simple controller exercises

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.