PC Review


Reply
Thread Tools Rating: Thread Rating: 4 votes, 1.00 average.

Catching 404 errors at Application Level

 
 
JJ
Guest
Posts: n/a
 
      26th May 2007
I only want to catch 404 errors at the application level (the rest are will
be handled by the customerrors section of the web.config). How do I check
for the error code in the
Application_Error of Global.asax ?

Thanks,
JJ


 
Reply With Quote
 
 
 
 
Juan T. Llibre
Guest
Posts: n/a
 
      26th May 2007


--
Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en espaņol : http://asp.net.do/foros/
======================================
"JJ" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
>I only want to catch 404 errors at the application level (the rest are will be handled by the
>customerrors section of the web.config). How do I check for the error code in the
> Application_Error of Global.asax ?
>
> Thanks,
> JJ
>



 
Reply With Quote
 
Juan T. Llibre
Guest
Posts: n/a
 
      26th May 2007
Sorry about the previous blank reply...

re:
!> How do I check for the error code in the Application_Error of Global.asax ?

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
Server.Transfer("Errors.aspx")
End Sub

Then, in Errors.aspx :
---------------------------------
<html>
<script language="VB" runat="server">
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load
Dim errMessage As String = ""
Dim appException As System.Exception = Server.GetLastError()
If (TypeOf (appException) Is HttpException) Then
Dim checkException As HttpException = CType(appException, HttpException)
Select Case checkException.GetHttpCode
Case 400
errMessage &= "Bad request. The file size is too large."
Case 401
errMessage &= "You are not authorized to view this page."
Case 403
errMessage &= "You are not allowed to view that page."
Case 404
errMessage &= "The page you have requested can't be found."
Case 408
errMessage &= "The request has timed out."
Case 500
errMessage &= "The server can't fulfill your request."
Case Else
errMessage &= "The server has experienced an error."
End Select
Else
errMessage &= "The following error occurred<BR>" & appException.ToString
End If
ErrorMessage.Text = errMessage & "<BR>We're sorry for the inconvenience."
Server.ClearError()
End Sub
</script>

<body>
<hr>
<asp:label id="ErrorMessage" font-size="12" font-bold="true" runat=server/>
<hr>
<p>Return to <a href="/">Your application's entry page</a>
</body>
</html>
-------------

You don't need to catch all those errors if you don't want to, but why only
catch 404 errors when all the ones listed in the Case statement are available ?




Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en espaņol : http://asp.net.do/foros/
======================================
"JJ" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
>I only want to catch 404 errors at the application level (the rest are will be handled by the
>customerrors section of the web.config). How do I check for the error code in the
> Application_Error of Global.asax ?
>
> Thanks,
> JJ
>




 
Reply With Quote
 
Mark Rae
Guest
Posts: n/a
 
      26th May 2007
"Juan T. Llibre" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Sorry about the previous blank reply...
>
> re:
> !> How do I check for the error code in the Application_Error of
> Global.asax ?
>
> Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
> Server.Transfer("Errors.aspx")
> End Sub
>
> Then, in Errors.aspx :
> ---------------------------------
> <html>
> <script language="VB" runat="server">
> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Load
> Dim errMessage As String = ""
> Dim appException As System.Exception = Server.GetLastError()
> If (TypeOf (appException) Is HttpException) Then
> Dim checkException As HttpException = CType(appException,
> HttpException)
> Select Case checkException.GetHttpCode
> Case 400
> errMessage &= "Bad request. The file size is too large."
> Case 401
> errMessage &= "You are not authorized to view this page."
> Case 403
> errMessage &= "You are not allowed to view that page."
> Case 404
> errMessage &= "The page you have requested can't be found."
> Case 408
> errMessage &= "The request has timed out."
> Case 500
> errMessage &= "The server can't fulfill your request."
> Case Else
> errMessage &= "The server has experienced an error."
> End Select
> Else
> errMessage &= "The following error occurred<BR>" &
> appException.ToString
> End If
> ErrorMessage.Text = errMessage & "<BR>We're sorry for the
> inconvenience."
> Server.ClearError()
> End Sub
> </script>
>
> <body>
> <hr>
> <asp:label id="ErrorMessage" font-size="12" font-bold="true"
> runat=server/>
> <hr>
> <p>Return to <a href="/">Your application's entry page</a>
> </body>
> </html>
> -------------
>
> You don't need to catch all those errors if you don't want to, but why
> only
> catch 404 errors when all the ones listed in the Case statement are
> available ?
>
>
>
>
> Juan T. Llibre, asp.net MVP
> asp.net faq : http://asp.net.do/faq/
> foros de asp.net, en espaņol : http://asp.net.do/foros/
> ======================================
> "JJ" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>>I only want to catch 404 errors at the application level (the rest are
>>will be handled by the
>>customerrors section of the web.config). How do I check for the error
>>code in the
>> Application_Error of Global.asax ?
>>
>> Thanks,
>> JJ
>>

>
>
>




--
http://www.markrae.net

 
Reply With Quote
 
Mark Rae
Guest
Posts: n/a
 
      26th May 2007
"Juan T. Llibre" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...

> Case 404
> errMessage &= "The page you have requested can't be found."


Doesn't this only work for pages which are processed by ASP.NET e.g. *.aspx
etc...?

Would this still work for something like:
http://www.mydomain.com/thispagedoesntexist.htm


--
http://www.markrae.net

 
Reply With Quote
 
Juan T. Llibre
Guest
Posts: n/a
 
      26th May 2007
re:
!> Would this still work for something like:
!> http://www.mydomain.com/thispagedoesntexist.htm

No, it wouldn't...unless you add *.htm/*.html to the aspnet_isapi.dll file mappings.

Most asp.net developers don't run .htm/.html files, though.
If they do, adding them will take care of the problem.




Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en espaņol : http://asp.net.do/foros/
======================================
"Mark Rae" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> "Juan T. Llibre" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
>
>> Case 404
>> errMessage &= "The page you have requested can't be found."

>
> Doesn't this only work for pages which are processed by ASP.NET e.g. *.aspx etc...?


> Would this still work for something like: http://www.mydomain.com/thispagedoesntexist.htm



 
Reply With Quote
 
Mark Rae
Guest
Posts: n/a
 
      26th May 2007
"Juan T. Llibre" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> re:
> !> Would this still work for something like:
> !> http://www.mydomain.com/thispagedoesntexist.htm
>
> No, it wouldn't...unless you add *.htm/*.html to the aspnet_isapi.dll file
> mappings.


That's what I thought...

> Most asp.net developers don't run .htm/.html files, though.
> If they do, adding them will take care of the problem.


Indeed - was just checking... :-)


--
http://www.markrae.net

 
Reply With Quote
 
Juan T. Llibre
Guest
Posts: n/a
 
      26th May 2007
re:
!> Indeed - was just checking... :-)

Much appreciated, as I had not made that clear.

One more thing I should add is that the sample code will *only* catch HttpException errors.

Coding exception catching should still be programmed into the page,
using Try, Catch, Finally syntax.




Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en espaņol : http://asp.net.do/foros/
======================================
"Mark Rae" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> "Juan T. Llibre" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>> re:
>> !> Would this still work for something like:
>> !> http://www.mydomain.com/thispagedoesntexist.htm
>>
>> No, it wouldn't...unless you add *.htm/*.html to the aspnet_isapi.dll file mappings.

>
> That's what I thought...
>
>> Most asp.net developers don't run .htm/.html files, though.
>> If they do, adding them will take care of the problem.

>
> Indeed - was just checking... :-)
>
>
> --
> http://www.markrae.net



 
Reply With Quote
 
JJ
Guest
Posts: n/a
 
      26th May 2007
Thanks for that. Unfortunately, it has exposed another problem:

The solution works well for when a user tries to access an aspx page that
does not exist, but when I type a partial directory name in the url like:

http://www.mysite.com/testdirecto

The error page gets displayed, but with no theme and loss of all the
graphics (like the page has lost its reference to the themes folder).
When debugging the Global.asax line 'Server.Transfer("~/Error.aspx")' is run
several times when entring a url like the one above, yet on typing a full
reference to a non existent aspx page the line only gets run once and the
error page displays correctly..

Any ideas what the problem is here?

Many thanks,

JJ


"Juan T. Llibre" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Sorry about the previous blank reply...
>
> re:
> !> How do I check for the error code in the Application_Error of
> Global.asax ?
>
> Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
> Server.Transfer("Errors.aspx")
> End Sub
>
> Then, in Errors.aspx :
> ---------------------------------
> <html>
> <script language="VB" runat="server">
> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Load
> Dim errMessage As String = ""
> Dim appException As System.Exception = Server.GetLastError()
> If (TypeOf (appException) Is HttpException) Then
> Dim checkException As HttpException = CType(appException,
> HttpException)
> Select Case checkException.GetHttpCode
> Case 400
> errMessage &= "Bad request. The file size is too large."
> Case 401
> errMessage &= "You are not authorized to view this page."
> Case 403
> errMessage &= "You are not allowed to view that page."
> Case 404
> errMessage &= "The page you have requested can't be found."
> Case 408
> errMessage &= "The request has timed out."
> Case 500
> errMessage &= "The server can't fulfill your request."
> Case Else
> errMessage &= "The server has experienced an error."
> End Select
> Else
> errMessage &= "The following error occurred<BR>" &
> appException.ToString
> End If
> ErrorMessage.Text = errMessage & "<BR>We're sorry for the
> inconvenience."
> Server.ClearError()
> End Sub
> </script>
>
> <body>
> <hr>
> <asp:label id="ErrorMessage" font-size="12" font-bold="true"
> runat=server/>
> <hr>
> <p>Return to <a href="/">Your application's entry page</a>
> </body>
> </html>
> -------------
>
> You don't need to catch all those errors if you don't want to, but why
> only
> catch 404 errors when all the ones listed in the Case statement are
> available ?
>
>
>
>
> Juan T. Llibre, asp.net MVP
> asp.net faq : http://asp.net.do/faq/
> foros de asp.net, en espaņol : http://asp.net.do/foros/
> ======================================
> "JJ" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>>I only want to catch 404 errors at the application level (the rest are
>>will be handled by the
>>customerrors section of the web.config). How do I check for the error
>>code in the
>> Application_Error of Global.asax ?
>>
>> Thanks,
>> JJ
>>

>
>
>



 
Reply With Quote
 
JJ
Guest
Posts: n/a
 
      26th May 2007
I see that, as pointed out by Mark that Server.Transfer doesn't work for non
aspx pages.
This problem raises its head when you try entering a non exisitent url
without the '.aspx'.

I would guess that at the 'Global.asax' level I need to check if the
referring url has a '.aspx' extension on it and if it doesn't, use
Response.Redirect instead.
I'm currently trying to work out how I do that....

I'm beginning to confuse myself as to why using the customErrors section of
the web.config is not enough in itself. I guess this doesn't kick in when an
unhandled expception is encountered...

JJ


"JJ" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Thanks for that. Unfortunately, it has exposed another problem:
>
> The solution works well for when a user tries to access an aspx page that
> does not exist, but when I type a partial directory name in the url like:
>
> http://www.mysite.com/testdirecto
>
> The error page gets displayed, but with no theme and loss of all the
> graphics (like the page has lost its reference to the themes folder).
> When debugging the Global.asax line 'Server.Transfer("~/Error.aspx")' is
> run several times when entring a url like the one above, yet on typing a
> full reference to a non existent aspx page the line only gets run once and
> the error page displays correctly..
>
> Any ideas what the problem is here?
>
> Many thanks,
>
> JJ
>
>
> "Juan T. Llibre" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
>> Sorry about the previous blank reply...
>>
>> re:
>> !> How do I check for the error code in the Application_Error of
>> Global.asax ?
>>
>> Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
>> Server.Transfer("Errors.aspx")
>> End Sub
>>
>> Then, in Errors.aspx :
>> ---------------------------------
>> <html>
>> <script language="VB" runat="server">
>> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
>> System.EventArgs) Handles MyBase.Load
>> Dim errMessage As String = ""
>> Dim appException As System.Exception = Server.GetLastError()
>> If (TypeOf (appException) Is HttpException) Then
>> Dim checkException As HttpException = CType(appException,
>> HttpException)
>> Select Case checkException.GetHttpCode
>> Case 400
>> errMessage &= "Bad request. The file size is too large."
>> Case 401
>> errMessage &= "You are not authorized to view this page."
>> Case 403
>> errMessage &= "You are not allowed to view that page."
>> Case 404
>> errMessage &= "The page you have requested can't be found."
>> Case 408
>> errMessage &= "The request has timed out."
>> Case 500
>> errMessage &= "The server can't fulfill your request."
>> Case Else
>> errMessage &= "The server has experienced an error."
>> End Select
>> Else
>> errMessage &= "The following error occurred<BR>" &
>> appException.ToString
>> End If
>> ErrorMessage.Text = errMessage & "<BR>We're sorry for the
>> inconvenience."
>> Server.ClearError()
>> End Sub
>> </script>
>>
>> <body>
>> <hr>
>> <asp:label id="ErrorMessage" font-size="12" font-bold="true"
>> runat=server/>
>> <hr>
>> <p>Return to <a href="/">Your application's entry page</a>
>> </body>
>> </html>
>> -------------
>>
>> You don't need to catch all those errors if you don't want to, but why
>> only
>> catch 404 errors when all the ones listed in the Case statement are
>> available ?
>>
>>
>>
>>
>> Juan T. Llibre, asp.net MVP
>> asp.net faq : http://asp.net.do/faq/
>> foros de asp.net, en espaņol : http://asp.net.do/foros/
>> ======================================
>> "JJ" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>>I only want to catch 404 errors at the application level (the rest are
>>>will be handled by the
>>>customerrors section of the web.config). How do I check for the error
>>>code in the
>>> Application_Error of Global.asax ?
>>>
>>> Thanks,
>>> JJ
>>>

>>
>>
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
catching all errors =?Utf-8?B?cm9kY2hhcg==?= Microsoft ASP .NET 6 20th Nov 2006 01:19 PM
Catching errors cbh35711 Microsoft Excel Programming 0 3rd Apr 2006 09:52 PM
catching Exceptions at a top level Laxmikant Rashinkar Microsoft C# .NET 4 15th Feb 2005 07:41 PM
Page Level and Applicatoin Level Custom Errors rranveer@gmail.com Microsoft ASP .NET 2 13th Feb 2005 02:03 AM
Catching ASP.NET errors Mikael Engdahl Microsoft ASP .NET 3 15th Aug 2003 07:18 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:02 PM.