473,406 Members | 2,894 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Trapping for invalid URLs

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
Nov 18 '05 #1
11 2390
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/de...lrewriting.asp

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

sayed


"Mark Rae" <ma**@mark-N-O-S-P-A-M-rae.co.uk> wrote in message news:<OR**************@TK2MSFTNGP12.phx.gbl>...
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

Nov 18 '05 #2
"Sayed Hashimi" <ha***********@hotmail.com> wrote in message
news:3a**************************@posting.google.c om...

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/de...lrewriting.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.
Nov 18 '05 #3
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" <ma**@mark-N-O-S-P-A-M-rae.co.uk> wrote in message
news:OR**************@TK2MSFTNGP12.phx.gbl...
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

Nov 18 '05 #4
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"
<ma**@mark-N-O-S-P-A-M-rae.co.uk> wrote:
"Sayed Hashimi" <ha***********@hotmail.com> wrote in message
news:3a**************************@posting.google. com...

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/de...lrewriting.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.


Nov 18 '05 #5
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" <us**@domain.com> wrote in message
news:yO******************@tornado.tampabay.rr.com. ..
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" <ma**@mark-N-O-S-P-A-M-rae.co.uk> wrote in message
news:OR**************@TK2MSFTNGP12.phx.gbl...
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


Nov 18 '05 #6
"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:vk********************************@4ax.com...
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...
Nov 18 '05 #7
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" <ma**@mark-N-O-S-P-A-M-rae.co.uk> wrote in message
news:OG**************@TK2MSFTNGP14.phx.gbl...
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" <us**@domain.com> wrote in message
news:yO******************@tornado.tampabay.rr.com. ..
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" <ma**@mark-N-O-S-P-A-M-rae.co.uk> wrote in message
news:OR**************@TK2MSFTNGP12.phx.gbl...
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



Nov 18 '05 #8
"Craig" <us**@domain.com> wrote in message
news:26*******************@tornado.tampabay.rr.com ...
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.
Nov 18 '05 #9
"Craig" <us**@domain.com> wrote in message
news:26*******************@tornado.tampabay.rr.com ...

What happens if you change:
Dim strOldPath As String = "/test.aspx"
to
Dim strOldPath As String = "/test.htm"

Nov 18 '05 #10
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" <ma**@mark-N-O-S-P-A-M-rae.co.uk> wrote in message
news:O0**************@TK2MSFTNGP10.phx.gbl...
"Craig" <us**@domain.com> wrote in message
news:26*******************@tornado.tampabay.rr.com ...

What happens if you change:
Dim strOldPath As String = "/test.aspx"


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


Nov 18 '05 #11
"Craig" <us**@domain.com> wrote in message
news:A0******************@tornado.tampabay.rr.com. ..
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...
Nov 18 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Kaidi | last post by:
Hello guys, I get the "an invalid XML character" error when using xerces to parse a XML file. I know that XML will correspond the &, <, >, " to special strings like "&gt;&lt;". However, how about if the...
22
by: Trevor Orton | last post by:
Hello, I'm having a slight problem using the W3C html validator and I've reviewed the FAQ's with no luck so hopefully someone here would be kind enough to point me in the right direction. I...
3
by: Paul | last post by:
I have an Access 2000 database with a form that is giving me some major headaches. When you open the form, it displays all records and allows editing, but has AllowAdditions set to False so that...
3
by: windandwaves | last post by:
Hi Gurus Does anyone know how I set the error trapping to option 2 in visual basic. I know that you can go to tools, options and then choose on unhandled errors only, but is there a VB command...
13
by: Thelma Lubkin | last post by:
I use code extensively; I probably overuse it. But I've been using error trapping very sparingly, and now I've been trapped by that. A form that works for me on the system I'm using, apparently...
19
by: KKramsch | last post by:
One of the features from other languages that I miss most in C is trappable exceptions. More specifically, I think it's great to be able to demarcate a whole block of code where several exceptions...
4
by: Mike | last post by:
I have a C# aspx page that calls a C# class file for db access, data, etc. If one of the "functions" I call in the class has an error how can i pass that error back to the aspx page and then...
6
by: Avi Kak | last post by:
Hello: Suppose I write a function that I want to be called with ONLY keyword argumnts, how do I raise an exception should the function get called with what look like position-specfic arguments?...
9
by: 47computers | last post by:
Pretty new to PHP, I recently started learning about error trapping. As of right now, I include the following into a page in my website: -------BEGIN PASTE-------- error_reporting(E_ERROR |...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.