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