Monday, December 21, 2009

ASP.NET MVC – Understanding Routes

Basically what I’m creating is a new web site for my wife’s photography company. Obviously I should’ve made a series out of my last several posts because they all relate to one another. Who knew I’d be learning so much and wanting to share? Anyhow, since I didn’t make it into a series, I’ll just link the related posts below.

  1. jQuery AJAX with ASP.NET MVC
  2. Beauty of Interfaces
  3. Learning Fluent NHibernate
  4. Refactoring my MVC & Photo Repository
  5. StructureMap with ASP.NET MVC

So let’s talk routes! This has got to be one of the coolest things about MVC. The amount of control you have over EVERYTHING is awesome! If you’ve ever created a MVC application, you know this default route:

"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults

Basically this says route name is “Default” and the URL is constructed by your controller name then the action and then the id and then the new {} sets the default controller and action with a blank id. Pretty simple and you can use it for most things, although on my wife’s site, I’m pretty sure I overrode all my routes to create simpler URLs. However, I did go ahead and leave the default route.

So my wife didn’t want to see /information/about or /information/help in the URL for her customers, so I had to modify my route so her users could go straight to /about and /help. The route to do something like that looks like this:

new {controller = "Information"});

This is calling my Information controller and passing whichever action is required. Pretty simple. Okay, so let’s continue.

She also wanted her customers to just go to /contact instead of /connect/contact, but I didn’t really want to map everything like that because her join mailing list calls /connect/joinmailinglist and I wanted to keep that route the same. So I created a static route just for contact, which looks like this:

new { controller = "Connect", action = "Contact" }

This route is called “Contact” and it’s set to static /contact and it goes straight to the connect controller and contact action.

So these few routes are really simple. Let me show you one that I think is really powerful, especially for her site. In order to create her portfolio, we wanted her users to go straight to portfolio/nameofportfolio. The route looks like this:

new { controller = "Portfolio", action = "Index" }

This is saying go to portfolio/nameofportfolio, which calls the portfolio controller and index action. When you do something like this, your parameter name on index has to match name like on line 3 of the above code. So my index action on the portfolio controller looks like this:

        public ActionResult Index(string name)
var pv = new PortfolioView {Name = name, Photos = _photorepository.GetPhotosBy(name)};
return View(pv);

What this gives my wife the power to do is create new portfolios all day long. All she has to do is make sure to tag her pics on flickr. To see how we’re calling her pics from flickr, see this post. So if she wants to send out a preview to her customers, she simply uploads to flickr and tags the pics with their last name or some random code and then sends them the /portfolio/lastname URL. We did pay for the pro membership to flickr, so we can get the original formats and connect to private pics.

I’m pretty sure the only place I read about routes was in the ASP.NET MVC in Action book. They have a whole chapter dedicated to routing. Great book! I don’t know how many times I’ve referenced it in my posts now…probably a lot.

That’s it! As always, please comment if you see anywhere that I could improve or you just have a comment. Thanks for reading!

kick it on

blog comments powered by Disqus
Related Posts Plugin for WordPress, Blogger...