RewritePath

Discussion in 'ASP .Net' started by Guest, Nov 9, 2006.

  1. Guest

    Guest Guest

    Hi everybody,
    I'm developing a simple web site for a friend of mine and I need an help for
    the folloeing situation: I am planning to map friendly page name (like
    '/faq.aspx') to more complicated ones (like '/displaypage.aspx?id=1'). I've
    done an internet search and I found that I can achieve this using httpModule.
    My first attempt was to using RwritePath in the BeginRequest event handler,
    retrieving the id associated to the friendly page and rewrite the path (using
    RewritePath) with the "complicated" one. Everything worked fine.
    Then I thought to optimize the site in order to reduce the number of
    connection to the database which stores the link between friendly pages and
    ids: to do this I thought to put the Dictionary containing the associations
    into the Session object in order to avoid connecting to the db after the
    first load.
    To do this I moved all the code from the BeginRequest handler to the
    PostAcquireRequestState one. Everything seems to work fine but the
    RewritePath as no effect.
    Here's the bit of code:

    private void context_PostAcquireRequestState(object sender, System.EventArgs
    e)
    {
    String RequestedUrl =
    HttpContext.Current.Request.Path.ToLowerInvariant();

    if (System.IO.Path.GetExtension(RequestedUrl)==".aspx")
    {
    String ButtonKey =
    RequestedUrl.Substring(RequestedUrl.LastIndexOf("/") + 1);
    if (HttpContext.Current.Session["MenubarButtonsList"] == null)
    {
    HttpContext.Current.Session["MenubarButtonsList"] =
    UserComponent.GetMenubarButtonsList(HttpContext.Current);
    }
    MenubarButtonsList ButtonsList =
    (MenubarButtonsList)HttpContext.Current.Session["MenubarButtonsList"];

    if (ButtonsList.ContainsKey(ButtonKey))
    {
    MenubarButton ReqButton = ButtonsList[ButtonKey];
    if (ReqButton.RewritePath)
    {
    System.Text.StringBuilder NewUrl = new
    System.Text.StringBuilder(HttpContext.Current.Request.ApplicationPath);
    NewUrl.Append("/PageDisplayer.aspx?id=");
    NewUrl.Append(ReqButton.Id);
    HttpContext.Current.RewritePath(NewUrl.ToString());
    }
    }
    }
    }


    Any idea??
    Thanks.

    Massimo Necchi
     
    Guest, Nov 9, 2006
    #1
    1. Advertisements

  2. Don't worry about writing this yourself, you can find some great free code
    to handle this, and more. I've use the UrlRewriting module from
    http://www.urlrewriting.net/en/Default.aspx on a bunch of applications.
    What's nice is you can even through regular expression matching into it.

    In ASP.Net 2.0 though, some of this is built right in. You can already map
    the faq.aspx page to something like your displaypage.aspx?id=1 with just a
    section of the config file. Search for info on the new urlMappings section
    of the web.config.
     
    Mark Fitzpatrick, Nov 9, 2006
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.