asp.net MVC and RESTful routing, rails-style. Is i

2019-03-27 20:07发布

问题:

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?

回答1:

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.