Trapping for invalid URLs

Discussion in 'ASP .Net' started by Mark Rae, Oct 4, 2004.

  1. Mark Rae

    Mark Rae Guest

    Hi,

    I've inherited an ASP.NET web app which I want to restructure into a more
    logical folder hierarchy (over 150 WebForms in the app's root), but lots of
    users access the site by fully-qualified URLs which they've added to their
    Favourites.

    Is there any way to trap a page request before it's processed so that I can
    redirect to its location in the new folder structure? E.g. user tries to
    access:

    http://www.mysite.com/viewmonthlystats.aspx

    but that page now resides in

    http://www.mysite.com/stats/monthly/view.aspx

    I'd hoped that I could do something with Application_BeginRequest in
    Global.asax, but I get the 404 error before that is even invoked.

    Mark
     
    Mark Rae, Oct 4, 2004
    #1
    1. Advertising

  2. Mark,

    The easiest way to do what you want is to implement a custom HTTP
    Handler. Before you do that though, I recommend that you have a look
    at the following article.

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/urlrewriting.asp

    It discusses, and provides a solution for, URL rewriting.

    sayed




    "Mark Rae" <> wrote in message news:<>...
    > Hi,
    >
    > I've inherited an ASP.NET web app which I want to restructure into a more
    > logical folder hierarchy (over 150 WebForms in the app's root), but lots of
    > users access the site by fully-qualified URLs which they've added to their
    > Favourites.
    >
    > Is there any way to trap a page request before it's processed so that I can
    > redirect to its location in the new folder structure? E.g. user tries to
    > access:
    >
    > http://www.mysite.com/viewmonthlystats.aspx
    >
    > but that page now resides in
    >
    > http://www.mysite.com/stats/monthly/view.aspx
    >
    > I'd hoped that I could do something with Application_BeginRequest in
    > Global.asax, but I get the 404 error before that is even invoked.
    >
    > Mark
     
    Sayed Hashimi, Oct 4, 2004
    #2
    1. Advertising

  3. Mark Rae

    Mark Rae Guest

    "Sayed Hashimi" <> wrote in message
    news:...

    Sayed,

    > The easiest way to do what you want is to implement a custom HTTP
    > Handler. Before you do that though, I recommend that you have a look
    > at the following article.
    >
    > http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/urlrewriting.asp
    >
    > It discusses, and provides a solution for, URL rewriting.


    Thanks for the reply - I checked this article out first but didn't think it
    would help me in this instance, due to the caveat at the end:

    "Of course, with ASP.NET-level rewriting, the URL rewriting can only happen
    if the request is
    successfully handed off from IIS to the ASP.NET engine. This naturally
    occurs when the user
    requests a page with a .aspx extension. However, if you want the person to
    be able to enter a
    URL that might not actually exist, but would rather rewrite to an existing
    ASP.NET page, you
    have to either create mock directories and Default.aspx pages, or configure
    IIS so that all incoming
    requests are blindly routed to the ASP.NET engine."

    I can't reconfigure IIS in this instance because its a public site being
    hosted with a commercial ISP who, understandably, won't allow this. I
    realise that I could leave the site's root directory full of tiny little
    HTML files which do nothing other than redirect to the equivalent page in
    the proposed new hierarchical structure, but was keen to avoid this if at
    all possible.
     
    Mark Rae, Oct 4, 2004
    #3
  4. Mark Rae

    Craig Guest

    Application_BeginRequest *is* where you should do this. I have several web
    sites that use urls for pages that don't exist. For example,
    http://mysite.com/mynewsstory.aspx for
    http://mysite.com/content/content.aspx?storyid=13.

    Something like this should work for you.
    Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
    ' Fires at the beginning of each request
    Try
    ' get the actual url posted by the user
    Dim strPath as String = Request.RawUrl
    Dim strMyNewPath as String = ""
    ' Do your work here to change the url to
    ' the path you really want
    ' ...
    Context.RewritePath(strMyNewPath)

    Catch ex As Exception
    ' your exception handler here
    HandleException()
    End Try
    End Sub

    HTH
    Craig

    "Mark Rae" <> wrote in message
    news:...
    > Hi,
    >
    > I've inherited an ASP.NET web app which I want to restructure into a more
    > logical folder hierarchy (over 150 WebForms in the app's root), but lots

    of
    > users access the site by fully-qualified URLs which they've added to their
    > Favourites.
    >
    > Is there any way to trap a page request before it's processed so that I

    can
    > redirect to its location in the new folder structure? E.g. user tries to
    > access:
    >
    > http://www.mysite.com/viewmonthlystats.aspx
    >
    > but that page now resides in
    >
    > http://www.mysite.com/stats/monthly/view.aspx
    >
    > I'd hoped that I could do something with Application_BeginRequest in
    > Global.asax, but I get the 404 error before that is even invoked.
    >
    > Mark
    >
    >
     
    Craig, Oct 4, 2004
    #4
  5. Mark Rae

    Scott Allen Guest

    Hi Mark:

    The ISP might be willing to modify the configuration for just your
    application, in which case, they could map requests to asp.net and
    uncheck the "Verify that file exists" checkbox for the .aspx script
    mapping on your vdir. My hosting provider allows this, it's common for
    web applications like the Community Startker Kit.

    --
    Scott
    http://www.OdeToCode.com/


    On Mon, 4 Oct 2004 13:36:04 +0100, "Mark Rae"
    <> wrote:

    >"Sayed Hashimi" <> wrote in message
    >news:...
    >
    >Sayed,
    >
    >> The easiest way to do what you want is to implement a custom HTTP
    >> Handler. Before you do that though, I recommend that you have a look
    >> at the following article.
    >>
    >> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/urlrewriting.asp
    >>
    >> It discusses, and provides a solution for, URL rewriting.

    >
    >Thanks for the reply - I checked this article out first but didn't think it
    >would help me in this instance, due to the caveat at the end:
    >
    >"Of course, with ASP.NET-level rewriting, the URL rewriting can only happen
    >if the request is
    >successfully handed off from IIS to the ASP.NET engine. This naturally
    >occurs when the user
    >requests a page with a .aspx extension. However, if you want the person to
    >be able to enter a
    >URL that might not actually exist, but would rather rewrite to an existing
    >ASP.NET page, you
    >have to either create mock directories and Default.aspx pages, or configure
    >IIS so that all incoming
    >requests are blindly routed to the ASP.NET engine."
    >
    >I can't reconfigure IIS in this instance because its a public site being
    >hosted with a commercial ISP who, understandably, won't allow this. I
    >realise that I could leave the site's root directory full of tiny little
    >HTML files which do nothing other than redirect to the equivalent page in
    >the proposed new hierarchical structure, but was keen to avoid this if at
    >all possible.
    >
     
    Scott Allen, Oct 4, 2004
    #5
  6. Mark Rae

    Mark Rae Guest

    Craig,

    Try this:

    Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)

    Dim strOldURL As String = "<the old page which no longer exists>"
    Dim strNewURL As String = "<the new page which does exist>"
    Dim strPath As String = Request.RawUrl

    If strPath = strOldURL Then
    Context.RewritePath(strMyNewPath)
    End If

    End Sub

    Set strOldURL to a webpage which doesn't exist in your website - what
    happens?

    Mark




    "Craig" <> wrote in message
    news:yOb8d.49764$...
    > Application_BeginRequest *is* where you should do this. I have several
    > web
    > sites that use urls for pages that don't exist. For example,
    > http://mysite.com/mynewsstory.aspx for
    > http://mysite.com/content/content.aspx?storyid=13.
    >
    > Something like this should work for you.
    > Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
    > ' Fires at the beginning of each request
    > Try
    > ' get the actual url posted by the user
    > Dim strPath as String = Request.RawUrl
    > Dim strMyNewPath as String = ""
    > ' Do your work here to change the url to
    > ' the path you really want
    > ' ...
    > Context.RewritePath(strMyNewPath)
    >
    > Catch ex As Exception
    > ' your exception handler here
    > HandleException()
    > End Try
    > End Sub
    >
    > HTH
    > Craig
    >
    > "Mark Rae" <> wrote in message
    > news:...
    >> Hi,
    >>
    >> I've inherited an ASP.NET web app which I want to restructure into a more
    >> logical folder hierarchy (over 150 WebForms in the app's root), but lots

    > of
    >> users access the site by fully-qualified URLs which they've added to
    >> their
    >> Favourites.
    >>
    >> Is there any way to trap a page request before it's processed so that I

    > can
    >> redirect to its location in the new folder structure? E.g. user tries to
    >> access:
    >>
    >> http://www.mysite.com/viewmonthlystats.aspx
    >>
    >> but that page now resides in
    >>
    >> http://www.mysite.com/stats/monthly/view.aspx
    >>
    >> I'd hoped that I could do something with Application_BeginRequest in
    >> Global.asax, but I get the 404 error before that is even invoked.
    >>
    >> Mark
    >>
    >>

    >
    >
     
    Mark Rae, Oct 4, 2004
    #6
  7. Mark Rae

    Mark Rae Guest

    "Scott Allen" <bitmask@[nospam].fred.net> wrote in message
    news:...

    > The ISP might be willing to modify the configuration for just your
    > application, in which case, they could map requests to asp.net and
    > uncheck the "Verify that file exists" checkbox for the .aspx script
    > mapping on your vdir. My hosting provider allows this, it's common for
    > web applications like the Community Startker Kit.


    I'll ask them, as I can't see any other solution...
     
    Mark Rae, Oct 4, 2004
    #7
  8. Mark Rae

    Craig Guest

    Hi Mark.

    I tried a test on one of my sites in an attempt to mirror your situation
    (i.e. files moved from the root of the site to subfolders). I created a
    file called test.aspx in the /test folder for my site. In my browser, I
    navigated to http://mysite.com/test.aspx. The code I have posted below
    rewrote the browser path to http://mysite.com/test/test.aspx.


    Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
    Try
    ' relative path to the url that does not exist
    Dim strOldPath As String = "/test.aspx"
    ' relative path to the url that DOES exist
    Dim strNewPath As String = "/test/test.aspx"
    ' relative path for the current request
    Dim strPath As String = Context.Request.RawUrl
    If strPath.ToLower.Equals(strOldPath) Then
    ' change the path variable to match the new path for the page
    strPath = strNewPath
    End If
    ' send the new path to the browser if appropriate
    Context.RewritePath(strPath)

    Catch ex As Exception
    ' your error handler here
    HandleError()
    End Try
    End Sub

    HTH
    Craig

    "Mark Rae" <> wrote in message
    news:...
    > Craig,
    >
    > Try this:
    >
    > Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
    >
    > Dim strOldURL As String = "<the old page which no longer exists>"
    > Dim strNewURL As String = "<the new page which does exist>"
    > Dim strPath As String = Request.RawUrl
    >
    > If strPath = strOldURL Then
    > Context.RewritePath(strMyNewPath)
    > End If
    >
    > End Sub
    >
    > Set strOldURL to a webpage which doesn't exist in your website - what
    > happens?
    >
    > Mark
    >
    >
    >
    >
    > "Craig" <> wrote in message
    > news:yOb8d.49764$...
    > > Application_BeginRequest *is* where you should do this. I have several
    > > web
    > > sites that use urls for pages that don't exist. For example,
    > > http://mysite.com/mynewsstory.aspx for
    > > http://mysite.com/content/content.aspx?storyid=13.
    > >
    > > Something like this should work for you.
    > > Sub Application_BeginRequest(ByVal sender As Object, ByVal e As

    EventArgs)
    > > ' Fires at the beginning of each request
    > > Try
    > > ' get the actual url posted by the user
    > > Dim strPath as String = Request.RawUrl
    > > Dim strMyNewPath as String = ""
    > > ' Do your work here to change the url to
    > > ' the path you really want
    > > ' ...
    > > Context.RewritePath(strMyNewPath)
    > >
    > > Catch ex As Exception
    > > ' your exception handler here
    > > HandleException()
    > > End Try
    > > End Sub
    > >
    > > HTH
    > > Craig
    > >
    > > "Mark Rae" <> wrote in message
    > > news:...
    > >> Hi,
    > >>
    > >> I've inherited an ASP.NET web app which I want to restructure into a

    more
    > >> logical folder hierarchy (over 150 WebForms in the app's root), but

    lots
    > > of
    > >> users access the site by fully-qualified URLs which they've added to
    > >> their
    > >> Favourites.
    > >>
    > >> Is there any way to trap a page request before it's processed so that I

    > > can
    > >> redirect to its location in the new folder structure? E.g. user tries

    to
    > >> access:
    > >>
    > >> http://www.mysite.com/viewmonthlystats.aspx
    > >>
    > >> but that page now resides in
    > >>
    > >> http://www.mysite.com/stats/monthly/view.aspx
    > >>
    > >> I'd hoped that I could do something with Application_BeginRequest in
    > >> Global.asax, but I get the 404 error before that is even invoked.
    > >>
    > >> Mark
    > >>
    > >>

    > >
    > >

    >
    >
     
    Craig, Oct 4, 2004
    #8
  9. Mark Rae

    Mark Rae Guest

    "Craig" <> wrote in message
    news:26h8d.68651$...

    > I tried a test on one of my sites in an attempt to mirror your situation
    > (i.e. files moved from the root of the site to subfolders). I created a
    > file called test.aspx in the /test folder for my site. In my browser, I
    > navigated to http://mysite.com/test.aspx. The code I have posted below
    > rewrote the browser path to http://mysite.com/test/test.aspx.


    On the live site, I just get the standard 404 "Page does not exist" error
    because IIS is verifying the existence of the URL before passing it to
    ASP.NET.
     
    Mark Rae, Oct 4, 2004
    #9
  10. Mark Rae

    Mark Rae Guest

    "Craig" <> wrote in message
    news:26h8d.68651$...

    What happens if you change:

    > Dim strOldPath As String = "/test.aspx"


    to

    > Dim strOldPath As String = "/test.htm"
     
    Mark Rae, Oct 4, 2004
    #10
  11. Mark Rae

    Craig Guest

    Since an html file will not begin an dotnet page request, I would get a 404
    error. I guess I didn't understand that the application you inherited was
    not a dotnet site.

    If you're changing the site from a static html site to a dotnet site, why
    not just create a custom 404 page that informs users that the site has
    changed and offer a link to a site map or something of the like? Then, have
    your site host use your new 404 page for those errors.

    Craig

    "Mark Rae" <> wrote in message
    news:...
    > "Craig" <> wrote in message
    > news:26h8d.68651$...
    >
    > What happens if you change:
    >
    > > Dim strOldPath As String = "/test.aspx"

    >
    > to
    >
    > > Dim strOldPath As String = "/test.htm"

    >
    >
     
    Craig, Oct 5, 2004
    #11
  12. Mark Rae

    Mark Rae Guest

    "Craig" <> wrote in message
    news:A0w8d.51689$...

    > Since an html file will not begin an dotnet page request, I would get a
    > 404
    > error. I guess I didn't understand that the application you inherited was
    > not a dotnet site.


    OK - my mistake for not making that explicity clear - apologies...

    > If you're changing the site from a static html site to a dotnet site, why
    > not just create a custom 404 page that informs users that the site has
    > changed and offer a link to a site map or something of the like? Then,
    > have
    > your site host use your new 404 page for those errors.


    Because the ISP doesn't allow me to do that...
     
    Mark Rae, Oct 5, 2004
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kaidi
    Replies:
    5
    Views:
    493
    Andrew Thompson
    Jan 4, 2004
  2. Josef Meile
    Replies:
    1
    Views:
    323
    Josef Meile
    Oct 13, 2004
  3. Nathan Sokalski

    Converting Relative URLs into Absolute URLs

    Nathan Sokalski, Aug 11, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    764
    Sriram Srivatsan
    Aug 12, 2008
  4. Adam Monsen

    JDBC URLs ...not really URLs?

    Adam Monsen, Feb 6, 2009, in forum: Java
    Replies:
    11
    Views:
    6,335
    Adam Monsen
    Feb 8, 2009
  5. Steve T.

    dynamic URLS convert to static URLS for search engines

    Steve T., Mar 1, 2004, in forum: ASP .Net Web Services
    Replies:
    7
    Views:
    311
    Steve T.
    Mar 4, 2004
Loading...

Share This Page