Trapping for invalid URLs

M

Mark Rae

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
 
M

Mark Rae

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.
 
C

Craig

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
 
S

Scott Allen

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.
 
M

Mark Rae

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
 
M

Mark Rae

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...
 
C

Craig

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
 
M

Mark Rae

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.
 
C

Craig

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
 
M

Mark Rae

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...
 

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top