Friday, September 19, 2014

Getting Started With ASP NET MVC 4 Part 2 (Adding A Controller)

---
Getting Started With ASP NET MVC 4 Part 2 (Adding A Controller)
STEPS
1) Open your MvcMovie solution
2) Add a new Controller
2-1) Right-click Controllers item, select Add/Controller…
2-2) Define Controller Settings
2-3) Notice the new HelloWorldController class and code editor window
2-4) Test Run
3) URL with Controller and Action
3-1) Add “/Welcome” to the current URL.
3-2) Stop the project execution.
4) URL with Controller, Action and Parameter
4-1) Replace the current Welcome method with the following:
4-2) Press F5 Key and Add “?name=Scott&numtimes=4” to the current URL.
4-3) Stop project execution.

STEPS

1) Open your MvcMovie solution

2) Add a new Controller

2-1) Right-click Controllers item, select Add/Controller…

2-2) Define Controller Settings

2-3) Notice the new HelloWorldController class and code editor window

2-4) Replace the code with the following:

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...";
        }
    }
}

2-4) Test Run

Press F5 Key (To run with Debugging mode).
Add “Hello World” to the default URL.

3) URL with Controller and Action

3-1) Add “/Welcome” to the current URL.

3-2) Stop the project execution.

4) URL with Controller, Action and Parameter

4-1) Replace the current Welcome method with the following:


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

4-2) Press F5 Key and Add “?name=Scott&numtimes=4” to the current URL.

4-3) Stop project execution.

---

---

No comments:

Post a Comment