- Download and install MVC 1.0.
- Create the MVC project.
- Startup VS.NET 2008.
- Click on File-->New-->Project.
- Choose the Web project type.
- Choose the ASP.NET MVC Web Application
- Click on OK.
- Create the Controller.
- In the Solution Explorer, right click on the Controllers tab, then click on Add-->Controller.
- Name it Test, then click on OK.
- Open up the Test1Controller.cs class file, in the Controllers folder.
- Modify the Index() method so that it looks like this:
public ActionResult Index(int ID) { return View(); } - Save it.
- Create the action.
- Right click inside of the Index method, then click on Add view...
- Leave the defaults, then click on Add.
- In the Views folder, notice that the Index.aspx page was added to the Test1 folder.
- Press Ctrl-Shift-B to compile the application.
- Press Ctrl-F5 to run the application.
- The following URL will map to the Test1 controller, action, and view: http://localhost:????/Test1/Index/1
Test1 is the controller. Index is the action, defined as a method inside the controller class, and 1 is the ID integer passed to that method. When that URL is process, the following method is called: Test1Controller.Index(1) - To make the ID integer options, convert the type from int to a nullable integer: int?
public ActionResult Index(int? ID) { return View(); } - After that, you should be able to omit the ID, like this: http://localhost:????/Test1/Index/ or http://localhost:????/Test1/
- Remember to compile the application every time a code change is performed.
Mike's a long-time, C# developer, with experience in database development, software architecture & design, .Net, ASP.NET, HTML, JavaScript, VB, CSS, Flash, SQL Server, Oracle, and more.
Monday, March 23, 2009
Getting Started with ASP.NET's MVC (Model View Controller)
Labels:
asp.net,
MVC,
VS.NET 2008
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment