7

Is there any way to get truly restful routing working in MVC, just like the rails dudes have? I 'm talking about nested urls like /bands/metallica/albums/killemall/track/4

The only library that I found to be useful is Steve Hodgkiss' Restful routing. It seems though a bit risky to base my whole project's routing on this guy's pet-project.

What say you MVC veterans?

4
  • 1
    Ain't it a little soon to have MVC "veterans" around? :) Commented Nov 3, 2009 at 18:17
  • Yes of course it is. But everything is possible when you're in the software engineering world! Commented Nov 3, 2009 at 18:50
  • Well, they were hiring positions requiring 3 years of C# experience. In 2002. I think that limited your hiring pool to Anders . . . Commented Nov 3, 2009 at 20:22
  • If it's open source (which it is) and it works (which it does) what's the problem? Commented Nov 22, 2009 at 14:41

1 Answer 1

9

Sure:

routes.MapRoute("IwannaBeLikeTheCoolRailsKids",
                "bands/{bandName}/albums/{albumName}/tracks/{trackNumber}",
                new { controller = "Bands",
                action = "ByTrack" 
               });

Then in your controller:

public ActionResult ByTrack(string bandName, string albumName, int trackNumber)

Easy peasie.

Sign up to request clarification or add additional context in comments.

12 Comments

Man, you wrote alblum all over the place! :)
How would you handle this url? /bands/metallica/albums/killemall/track/the-four-horsemen
without losing the int track url functionality
Spelling wasn't my strong suit. As for the second part, there isn't a way to remove the track number from the URL and still use it as a parameter. Rather, what you'd need to do is make it so you could find the song by a url slug field.
My thought is anyone who manually changes the url to try and find a result deserves a 404 if the there is nothing there. Why write several hours of extra code to accommodate someone who is too lazy to go through your UI to find what they are looking for. I agree, url's need to be user friendly, but don't break your back thinking and coding routes for someone who is tinkering with your urls outside of the UI.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.