I'd like to setup one of my Controllers to intercept "real" URLs that come by--in this example, a photo gallery that I'm using as a prototype that has an existing URL format of (/ViewGallery.aspx?galleryId=[id]). Unfortunately, I haven't been able to get query string parameters to work in the Routes (perhaps by design). Here's what I'm doing to work around that; is there a better way to do this?
1. I created a new Route that handles the Url that I'm looking for and sends it to a controller.
RouteTable.Routes.Add(new Route
{
Url = "ViewGallery.aspx",
Defaults = new
{
controller = "Home",
action = "ViewOldGallery",
},
RouteHandler = typeof(MvcRouteHandler)
});
2. I created an "OldGallery" action that redirects back to the new gallery; basically translating the galleryId to Id.
[ControllerAction]
public void ViewGallery(int id)
{
Gallery gallery = webgallery.GetGalleryById(id);
RenderView("ViewGallery", gallery);
}
[ControllerAction]
public void ViewOldGallery(int galleryId)
{
RedirectToAction(new
{
Action = "ViewGallery",
id = galleryId
});
}
This screams of inefficient and DRY unhappiness (or me not sure what I'm doing), but I can't find a way to get the querystrings to pass. I'd like to simply create a "route rule" that looks for a pattern, something like:
RouteTable.Routes.Add(new Route
{
Url = "ViewGallery.aspx?galleryId=[id]",
Defaults = new
{
controller = "Home",
action = "ViewGallery",
},
RouteHandler = typeof(MvcRouteHandler)
});
Any suggestions would be greatly appreciated! :)
-dl