We're migrating a legacy app to .NET MVC and have to maintain backwards compatibility for the old URLs. Consider a forum application where URLs look like this
http://example.com/forums?page=ForumsList - shows a list of all forums
http://example.com/forums?page=Threads&forumId=1 - shows list of threads in forum 1
http://example.com/forums?page=Posts&forumId=1&threadId=1 - shows posts in forum 1, thread 1.
I'd like to map those same URLs to actions in my controllers, i.e.
public ActionResult ForumsList(){}
public ActionResult Threads(string forumId){}
public ActionResult Posts(string forumId, string threadId){}
Without duplicating this maintenance nightmare that we had in our legacy system
public ActionResult Index(string page){
if (page == "Forums"){
return Forums();
}
else if (page == "Threads){
return Threads(Request.Params["forumId"]);
}
else if (page == "Posts"){
return Posts(Request.Params["forumId"], Request.Params["threadId"]);
}
// oh, there's more. a lot more.
}
All the documentation I can find on custom routing seems to suggest that it can only parse the path portion of a URL, not the params. I tried the following, to no avail:
routes.MapRoute(
name: "TestRoutes",
url: "{controller}/?page={action}",
defaults: new {controller = "Forums", action = "ForumsList"});
Anything else I can do?
forums?page=Threads&forumId=1to something like/forums/threads/1