tags:

views:

41

answers:

3

Let's say I have an URL that looks like this:

http://www.mysite.com/area/topic/subtopic/subsubtopic

where the number of topics and subtopics is arbitrary.

Is there a way to process this kind of URL in ASP.NET MVC?

+1  A: 

I think the only way to do it would be to map the area to {controller} and topic to {action} and then the subtopics would just have to be parameters on the action.

MVC does limit you to a 2-dimensional array of controller vs action.

Chris Arnold
It sounds like I would need several routes and several controller overloads.
Robert Harvey
No, the default one will work with what you've described above. The Model Binder will try to map any extra 'data' from the url to parameters on its chosen Action.
Chris Arnold
+1  A: 

You can pass arbitrary number of parameters to an action by using a wildcard-route, e.g. like "../{topic}/{subtopics*}". Everything specified after the topic in the URL will then be passed as is to the action's parameter and you can then split that value into the separate subtopics.

Scott Guthrie mentions that technique in this video at around 35:20.

M4N
Nice technique. If The Gu advocates it, it must be good.
Robert Harvey
+1  A: 

We do exactly this with reflection and the invoke method.

We've added a custom .net 404 handler to IIS and this handler take the parts of the url and invokes them along the lines of

namespace area{

class topic 
{

void subtopic(param object[] subsubtopic)
{
 //do page
}    
}

}

Obviously the exact mapping of url to method signature will vary.

So mydomain.com/pages/blogs/newblogs/tech or mydomain.com/pages/blogs/newblogs/health

might look like

namespace pages{

class blogs 
{

void newblogs(string subject)
{
 if(subject == "tech")
  subject = "ace";
 if(subject == "health")
  subject = "itch it";
}    
}

}

You might also want to look in to restful urls.

runrunraygun