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.
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:
The query string is not part of the routing. However, query string parameters are actually the default way to pass values to an action. For example, if you set your route to be:
[ControllerAction]
public void ViewOldGallery(string galleryId)
{
//do your stuff
}
That should just work. By default, if an action has parameters that are not specified in the route, then any query string parameters are used to try and match the action method parameters.
Phil Haack (http://haacked.com/)
Senior Program Manager, Microsoft
The query string is not part of the routing. However, query string parameters are actually the default way to pass values to an action. For example, if you set your route to be:
[ControllerAction]
public void ViewOldGallery(string galleryId)
{
//do your stuff
}
That should just work. By default, if an action has parameters that are not specified in the route, then any query string parameters are used to try and match the action method parameters.
Phil Haack (http://haacked.com/)
Senior Program Manager, Microsoft
tiredstudent
0 Points
2 Posts
Routes - Redirecting from "Real" URLs
Dec 12, 2007 09:28 PM|LINK
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.
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:
Any suggestions would be greatly appreciated! :)
-dl
MVC Routes
http://blog.tiredstudent.com
"I am lost. I have gone to find myself. If I should return before I get back, please have me wait."
Haacked
Contributor
6901 Points
412 Posts
Re: Routes - Redirecting from "Real" URLs
Dec 13, 2007 08:04 AM|LINK
The query string is not part of the routing. However, query string parameters are actually the default way to pass values to an action. For example, if you set your route to be:
RouteTable.Routes.Add(new Route { Url = "ViewGallery.aspx", Defaults = new { controller = "Home", action = "ViewOldGallery", }, RouteHandler = typeof(MvcRouteHandler) });And an action method like so:
[ControllerAction] public void ViewOldGallery(string galleryId) { //do your stuff } That should just work. By default, if an action has parameters that are not specified in the route, then any query string parameters are used to try and match the action method parameters.
Senior Program Manager, Microsoft
What wouldn’t you do for a Klondike bar?
Haacked
Contributor
6901 Points
412 Posts
Re: Routes - Redirecting from "Real" URLs
Dec 13, 2007 08:04 AM|LINK
The query string is not part of the routing. However, query string parameters are actually the default way to pass values to an action. For example, if you set your route to be:
RouteTable.Routes.Add(new Route { Url = "ViewGallery.aspx", Defaults = new { controller = "Home", action = "ViewOldGallery", }, RouteHandler = typeof(MvcRouteHandler) });And an action method like so:
[ControllerAction] public void ViewOldGallery(string galleryId) { //do your stuff } That should just work. By default, if an action has parameters that are not specified in the route, then any query string parameters are used to try and match the action method parameters.
Senior Program Manager, Microsoft
What wouldn’t you do for a Klondike bar?
tiredstudent
0 Points
2 Posts
Re: Routes - Redirecting from "Real" URLs
Dec 13, 2007 12:53 PM|LINK
Okay, that's what I did, so I'll go with that--just wanted to be sure. :)
Thanks for the clarification!
-dl
http://blog.tiredstudent.com
"I am lost. I have gone to find myself. If I should return before I get back, please have me wait."