locked
URL Remapping Problem RRS feed

  • Question

  • User1122355199 posted

    Hello Everyone and thanks for your help in advance.  I am new to URL Remapping and am trying to work through my first project.  Basically, I have a one page website where the default page is dynamically changed when various parameters in the querystring are passed.  The defaul page is in the root directory and appropraitely named Default.aspx.  I woulld like to create links that would be formatted something like /articles/1/aspx or resume/1.aspx with the idea that articles or resume would determine the category to be loaded and the 1 in the link would be the item in that subcategory.  To that end I have written the code below:


            Dim URLMatch As String = "Resume/.*"
            Dim URLMatch2 As String = "Articles/.*"

            ' Fires at the beginning of each request
            Dim URLRequest As HttpContext = HttpContext.Current
            Dim NewURL As String = ""
            Dim RequestedURL As String = URLRequest.Request.Path.ToLower
            RequestedURL = URLRequest.Request.AppRelativeCurrentExecutionFilePath


            If Regex.Match(RequestedURL, URLMatch).Success Then
                'Response.Write("Found " & CheckURL)
                NewURL = "/Default.aspx?CP=Resume"
                URLRequest.RewritePath(NewURL)
            ElseIf Regex.Match(RequestedURL, URLMatch2).Success Then
                NewURL = "/Default.aspx?CP=Articles"
                URLRequest.RewritePath(NewURL)
            End If

    On the default page, I have added the two links:

    <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="Resume/1.aspx">Click Here For Resume</asp:HyperLink>

    <asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl="Articles/2.aspx">Click Here For Articles</asp:HyperLink>

    Here is my probem.  For example, when I click on the link Articles for the first time, the address bar shows /Articles/1.aspx.  Then, if I click on the Resume link, the address bar shows /Resume/Article/1.aspx.  Then, each subsequent link click takes the address bar further down the hierarchy.  Additionally, since I am parsing for either resume or article, the parsing stops working correctly after the first click.  Obviously I haven't even begun to work on parsing out the "1" or "2" item numbers in the string.  I guess I have two questions.  First, how do i go aobut fixing this problem.  Second, is the whole concept of using one page and dynamically changing it like the a poor choice.  Any help would be greatly appreciated.  Thanks.

    Monday, July 16, 2007 5:41 AM

Answers

  • User1424575140 posted

    Over time, I've tried a couple different approaches and the factory has been the most dependable (least leaky).

    When a page is requested, the handler factory serves it as though it actualy existed at that location. 

    My sample may seem more complicated than it is because of the way things are abstracted.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 17, 2007 7:27 AM

All replies

  • User1424575140 posted

     Your current issue might just be the NavigateUrl="Resume/1.aspx" needing to be NavigateUrl="../Resume/1.aspx"

     I have an example using an http hanlder factory to do the equivalent of url rewriting.

    http://weblogs.asp.net/andrewseven/pages/UrlRewriting.aspx

    Monday, July 16, 2007 9:25 AM
  • User1122355199 posted

    First of all, thanks for the assistance.  I greatly appreciate it.  I took a look at your code and the one question I have is why use IHttpHandlerFactory at all since there appears to be simpler ways to reach the same result.  I'm sure there is a pretty good reason, but since I'm not really up to speed on this topic, the nuances are lost on me.  Can you please run me through this a little deeper?

    Once again, thanks for the assistance.

    Monday, July 16, 2007 8:37 PM
  • User1424575140 posted

    Over time, I've tried a couple different approaches and the factory has been the most dependable (least leaky).

    When a page is requested, the handler factory serves it as though it actualy existed at that location. 

    My sample may seem more complicated than it is because of the way things are abstracted.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 17, 2007 7:27 AM