views:

192

answers:

3

I would like to create routing rules like the following:

www.app.com/project/35/search/89/edit/48 ---> action is edit in the project controller

The passed variables should be project# (35), search# (89), and edit#(48)

Can someone help me structure a routes.MapRout() for this.

I want to use:

routes.MapRoute(
            "Default",
            "{controller}/{projectid}/search/{searchid}/{action}/{actionid}",
            new { controller = "Home", action = "Edit", projectid = "", actionid = "" }
        );

But from previous experience, this type of MapRoute will fail... I've only gotten something in the following format to work:

{controller}/{action}/{variable}

Can anyone give me any advice on this? Thanks.

A: 

Grouping Controllers with ASP.NET MVC

A question that often comes up is how do you group controllers when building a large application with ASP.NET MVC. Often, the question is phrased as whether or not ASP.NET MVC supports “Areas”, a feature of Monorail. According to the Monorail documentation,

MonoRail supports the concept of areas, which are logical groups of controllers. All controllers belong to an area. The default area is an empty (unnamed) one

While there’s no out of the box support for this in ASP.NET MVC, the extensibility model allows building something pretty close.

Registering Routes

The first thing we do is call two new extension methods I wrote to register routes for the areas. This call is made in the RegisterRoutes method in Global.asax.cs.

routes.MapAreas("{controller}/{action}/{id}", "AreasDemo", new[]{ "Blogs", "Forums" });

routes.MapRootArea("{controller}/{action}/{id}", "AreasDemo", new { controller = "Home", action = "Index", id = "" });The first argument to the MapAreas method is the Routing URL pattern you know and love. We will prepend an area to that URL. The second argument is a root namespace. By convention, we will append “.Areas.AreaName.Controllers” to the provided root namespace and use that as the namespace in which we lookup controller types.

http://haacked.com/archive/2008/11/04/areas-in-aspnetmvc.aspx

Robert Harvey
This only partially solves the problem. Adding a namespace will move the controller from Project to Search. But I still need to have edit in the URL. Also, in some cases, there will even be additional elements in the URL (e.g. www.app.com/project/35/search/89/edit/48/flag/63).
rksprst
rksprst, please see http://stackoverflow.com/questions/1080704/asp-net-routing-question/1081144#1081144
Robert Harvey
+1  A: 

Honestly, it sounds like you need to make you URL's look like this:

www.app.com/project/35?search=89&edit=48&flag=63

It would make your like much simpler.

Robert Harvey
A: 

Why did this format fail for you? What do you mean by "failed"? If it just didn't work, try placing your new MapRoute above the default commands - the Routes collection is working in a FIFO manner.

@Robert Harvey - Take a look at Basecamp for example - they are using a very similar approach in their URI's. I think it's a much cleaner and better approach than what you're suggesting. It's not even "Simpler" once you get the hang of routing.

synhershko