You'll need to convert this code to C#, but it works in VB.NET ...
In global.asax :
Sub Application_Err or(ByVal sender As Object, ByVal e As EventArgs)
Server.Transfer ("Errors.asp x")
End Sub
Errors.aspx :
--------------------
<html>
<script language="VB" runat="server">
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
Dim errMessage As String = ""
Dim appException As System.Exceptio n = Server.GetLastE rror()
If (TypeOf (appException) Is HttpException) Then
Dim checkException As HttpException = CType(appExcept ion, 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.To String
End If
ErrorMessage.Te xt = errMessage & "<BR>We're sorry for the inconvenience."
Server.ClearErr or()
End Sub
</script>
<body>
<hr>
<asp:label id="ErrorMessag e" font-size="12" font-bold="true" runat=server/>
<hr>
</body>
</html>
--------------
That traps 401, and the other listed errors, fine.
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/
=============== =============== ========
"rote" <na********@hot mail.comwrote in message news:%2******** ********@TK2MSF TNGP05.phx.gbl. ..
>
I'm using ASP.NET 2.0 and i have copied and pasted the code below to my Global.asax file but it desn't trap
the error
I want to trap the 401 access denied
void Application_Err or(object sender, EventArgs e)
{
Exception exception = Server.GetLastE rror();
try
{
HttpException httpException = (HttpException) exception;
int httpCode = httpException.G etHttpCode();
switch (httpCode)
{
case 401: Response.Redire ct("~/Pages/Error/NoAccess.aspx") ; break;
case 404: Response.Redire ct("~/Pages/Error/PageNotFound.as px"); break;
default: Response.Redire ct("~/Pages/Error/Generic.aspx"); break;
}
}
catch { }
Server.ClearErr or();
}
I know i can't use the normal custom error in the web config either beacuse it doesn't work
Any ideas how to trap this?
<customErrors mode="RemoteOnl y" defaultRedirect ="GenericErrorP age.htm">
<error statusCode="401 " redirect="acces sdenied.htm" ></customErrors>
HTTP Error 401.1 - Unauthorized: Access is denied due to invalid credentials.
Internet Information Services (IIS)