Page view counter

Routes - Redirecting from "Real" URLs

Last post 12-13-2007 8:53 AM by tiredstudent. 3 replies.

Sort Posts:

  • Routes - Redirecting from "Real" URLs

    12-12-2007, 5:28 PM
    • Loading...
    • tiredstudent
    • Joined on 09-20-2005, 2:42 PM
    • Wichita, KS
    • Posts 2

    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

    -David
    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."
    Filed under: ,
  • Re: Routes - Redirecting from "Real" URLs

    12-13-2007, 4:04 AM
    • Loading...
    • Haacked
    • Joined on 09-17-2003, 2:43 PM
    • Posts 343
    • AspNetTeam

    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.
    

     

    Phil Haack (http://haacked.com/)
    Senior Program Manager, Microsoft

    What wouldn’t you do for a Klondike bar?
  • Re: Routes - Redirecting from "Real" URLs

    12-13-2007, 4:04 AM
    • Loading...
    • Haacked
    • Joined on 09-17-2003, 2:43 PM
    • Posts 343
    • AspNetTeam

    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.
    

     

    Phil Haack (http://haacked.com/)
    Senior Program Manager, Microsoft

    What wouldn’t you do for a Klondike bar?
  • Re: Routes - Redirecting from "Real" URLs

    12-13-2007, 8:53 AM
    • Loading...
    • tiredstudent
    • Joined on 09-20-2005, 2:42 PM
    • Wichita, KS
    • Posts 2

    Okay, that's what I did, so I'll go with that--just wanted to be sure. :)

    Thanks for the clarification!

    -dl

    -David
    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."
Page 1 of 1 (4 items)
Microsoft Communities