Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net MVC and RESTful routing, rails-style. Is it possible?

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?

like image 389
thanos panousis Avatar asked Nov 03 '09 18:11

thanos panousis


People also ask

Is ASP.NET MVC RESTful?

ASP.NET MVC is one such framework that provides an inherently RESTful model for building XHTML-based services.

Which type of routing is preferred in MVC?

For controllers, the above configuration specifies a default routing convention, which is the fairly standard {controller}/{action}/{id?} pattern that's been recommended since the first versions of ASP.NET MVC.

How many types of routing are there in ASP.NET MVC?

There are two types of routing (after the introduction of ASP.NET MVC 5). Convention based routing - to define this type of routing, we call MapRoute method and set its unique name, url pattern and specify some default values.

What is routing in MVC and what is a default route?

The RegisterRoutes() method creates the route table. The default route table contains a single route (named Default). The Default route maps the first segment of a URL to a controller name, the second segment of a URL to a controller action, and the third segment to a parameter named id.


1 Answers

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.

like image 84
Wyatt Barnett Avatar answered Oct 04 '22 14:10

Wyatt Barnett