RewritePath doesn't change HttpContext.Current.Request

Last post 06-24-2009 7:13 PM by ken.smith. 2 replies.

Sort Posts:

  • RewritePath doesn't change HttpContext.Current.Request

    06-24-2009, 6:05 PM
    • Member
      6 point Member
    • ken.smith
    • Member since 02-19-2009, 11:55 AM
    • Woodinville, WA
    • Posts 7

    I'm using HttpContext.Current.RewritePath to change a URL that looks like this:

    domain.com/<userid>/<roomname>

    Into a URL that looks like this:

    domain.com/Room.aspx?userID=<userid>&roomName=<roomname>

    That bit works fine.  But in Room.aspx, I was then expecting to be able to access the QueryString parameters just like normal, i.e.:

    string userID = Request.QueryString["userID"];
    string roomName = Request.QueryString["roomName"];

    However, despite the fact that it's been rewritten, the "Request" object still has only the old information about the original, non-rewritten request, i.e., Request.Path = "/someuserid/someroomname", not "Room.aspx?userID=someuserid&roomName=someroomname".  That makes it just a tad bit trickier to pull out the parameters, since you have to do string parsing, but if you have multiple different URL structures that all map to the same .aspx page, it makes it kinda tricky to figure out which ones are which.

    Am I missing something here, or is this how it's intended to work?

    Ken Smith
    Filed under:
  • Re: RewritePath doesn't change HttpContext.Current.Request

    06-24-2009, 6:30 PM
    Answer
    • Member
      269 point Member
    • visliCom
    • Member since 05-01-2008, 4:57 PM
    • Posts 226
  • Re: RewritePath doesn't change HttpContext.Current.Request

    06-24-2009, 7:13 PM
    Answer
    • Member
      6 point Member
    • ken.smith
    • Member since 02-19-2009, 11:55 AM
    • Woodinville, WA
    • Posts 7

    If I understand your code correctly, that's how I expected it to work, i.e., you can actually do something like this:

    <rule source="(\w*)/(\w+)/?" destination="Room.aspx?ownerUserID=$1&amp;roomName=$2" />

    However, the code that I'm using (borrowed from here: http://www.simple-talk.com/dotnet/asp.net/a-complete-url-rewriting-solution-for-asp.net-2.0/) doesn't seem to work that way.  Here's a snippet:

                                // new path to rewrite to
                                string rew = cfg.RewriteBase + path;
                                // save original path to HttpContext for further use
                                HttpContext.Current.Items.Add("OriginalUrl", HttpContext.Current.Request.RawUrl);
                                // rewrite
                                Debug.WriteLine(string.Format("Rewriting {0} to {1}", HttpContext.Current.Request.Path, rew));
                                HttpContext.Current.RewritePath(rew);

    // new path to rewrite to
    string rew = cfg.RewriteBase + path;
    // save original path to HttpContext for further use
    HttpContext.Current.Items.Add("OriginalUrl", HttpContext.Current.Request.RawUrl);
    // rewrite
    Debug.WriteLine(string.Format("Rewriting {0} to {1}", HttpContext.Current.Request.Path, rew));
    HttpContext.Current.RewritePath(rew);
    

    Am I doing anything wrong?

    Ken Smith
Page 1 of 1 (3 items)