Please make sure you’ve seen my previous blog post because this one continues on the project built.
In this post, we’ll explore a couple of more options to learn more about MVC. There are some great starting points right here.
I will go through some of these, briefly. I will adapt the “HelloWorld” from my previous blog entry, which shows you how to setup and get started with your first application.
The goal of creating custom routes is to make interaction with your web site more intuitive and natural. It should also map well for search engines.
Scott Guthrie said it best at http://weblogs.asp.net/scottgu/archive/2007/12/03/asp-net-mvc-framework-part-2-url-routing.aspx
Imagine that you sell car parts:
Our goal is to support the following:
So here are the 4 steps we will take
How we would setup our own view and controller (basic starter kit)
The whole point here is to get us to the right controller / action method / custom view combination that we dream up based on url.
We want to support the url http://blah_blah.com/Brakes/Disc. That means we need to add our own controllers, action methods, and views.
Notice lines 27-31 where we added an additional routing table. You can start to see support for the http://blah_blah.com/Brakes/Disc url.
One of the first steps you need to do to get your url supported with the MVC plumbing is to add a custom controller.
It is easy to do because it is built into Visual Studio 2010 Beta 2. Once we add the controller (BrakesController) we will also need to add an action method called "Disc()".
Make sure your selections as follows:
Notice that lines 19-22 support the Disc part of http://blah_blah.com/Brakes/Disc
We will cover Details(), Create(), Edit() in a future post.
After we have added the controller and the action method, we need to next add our own custom view. Later we will play with the model and add database support.
Call the new folder Brakes. Make sure you create the folder underneath the Views folder.
Afterall, http://blah_blah.com/Brakes/Disc should take you to a database query that shows "Disc Brakes."
We can worry about CRUD later (Create, Read, Update, Delete).
Now add the view. Later we will enhance this view to cooperate with the model object to display data. In particular, to display disc brakes.
Call the view “Disc” to support http://blah_blah.com/Brakes/Disc.
Modify the code a little bit to validate the the view we create is the view we see.
This is the default view of Disc.aspx
Add your own code in the designer as follows for Disc.aspx (our view to support http://blah_blah.com/Brakes/Disc.
Finally, we want to run the application with http://blah_blah.com/Brakes/Disc and see our view popup with our own custom message to show the whole thing is working together correctly.
Run the final program. But we will need to edit the url.
This is the wrong url:
We’ve been talking about:
Notice the url we want: http://localhost:19078/Brakes/Disc
This represents the final step for this post. Hope you enjoyed it.
Here is the code: