Connecting Tech Pros Worldwide Forums | Help | Site Map

Read <customErrors> element via VB.NET Code?

=?Utf-8?B?TWlrZQ==?=
Guest
 
Posts: n/a
#1: Feb 8 '07
Hi. Is it programatically possible in VB.NET to read the contents of
web.config's <customErrorselement? I looked at using
ConfigurationSettings.AppSettings, but that doesn't work.

I need to read the value of redirect from the error statusCode 404. My
web.config looks like this:
<customErrors mode="RemoteOnly" defaultRedirect="Error.aspx">
<error statusCode="404" redirect="Error404.aspx" />
</customErrors>

Thanks!

=?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=
Guest
 
Posts: n/a
#2: Feb 8 '07

re: Read <customErrors> element via VB.NET Code?


Hi Mike,

using System.Web.Configuration;

// pass application virtual directory name
Configuration configuration =
WebConfigurationManager.OpenWebConfiguration("/TestWebsite");
CustomErrorsSection section =
(CustomErrorsSection)configuration.GetSection("sys tem.web/customErrors");

foreach (CustomError error in section.Errors)
{
Response.Write(error.StatusCode);
}
--
Milosz


"Mike" wrote:
Quote:
Hi. Is it programatically possible in VB.NET to read the contents of
web.config's <customErrorselement? I looked at using
ConfigurationSettings.AppSettings, but that doesn't work.
>
I need to read the value of redirect from the error statusCode 404. My
web.config looks like this:
<customErrors mode="RemoteOnly" defaultRedirect="Error.aspx">
<error statusCode="404" redirect="Error404.aspx" />
</customErrors>
>
Thanks!
Juan T. Llibre
Guest
 
Posts: n/a
#3: Feb 8 '07

re: Read <customErrors> element via VB.NET Code?


re:
Quote:
Hi. Is it programatically possible in VB.NET to read the contents of web.config's <customErrorselement?
Sure it's possible.

See it working at : http://asp.net.do/test/readCustomErrors.aspx

Here's the source for that sample I wrote :

readCustomErrors.aspx:
-----------------------------------

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Web.Configuration" %>
<%@ Import Namespace="System.Configuration" %>

<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

' Set the root path of the Web application that contains the Web.config file that you want to access.

Dim configuration As System.Configuration.Configuration = WebConfigurationManager.OpenWebConfiguration("/test")

' Get the section.

Dim customErrorsSection As CustomErrorsSection = CType(configuration.GetSection("system.web/customErrors"), CustomErrorsSection)

' Read the <customErrorssection mode.

Dim currentMode As CustomErrorsMode = customErrorsSection.Mode

' Display the <customErrorsinformation.

ConfigId.Text = currentMode.ToString()

End Sub
</script>

<html >
<head>
<title>Read customErrors Configuration Setting</title>
</head>
<body>
<p>
This page displays the <b>value</bof the <b>customErrors</bsection of web.config.
</p>
<h3>Value:</h3>
<p>
<asp:Label ID="ConfigId" BackColor="#dcdcdc" BorderWidth="1" runat="Server" /></p>
</body>
</html>
-----------

Enjoy!



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/
===================================
"Mike" <Mike@discussions.microsoft.comwrote in message news:5B4A9B56-8FDA-4045-991F-B87CA3CBE5F3@microsoft.com...
Quote:
Hi. Is it programatically possible in VB.NET to read the contents of
web.config's <customErrorselement? I looked at using
ConfigurationSettings.AppSettings, but that doesn't work.

I need to read the value of redirect from the error statusCode 404. My
web.config looks like this:
<customErrors mode="RemoteOnly" defaultRedirect="Error.aspx">
<error statusCode="404" redirect="Error404.aspx" />
</customErrors>

Thanks!
Juan T. Llibre
Guest
 
Posts: n/a
#4: Feb 8 '07

re: Read <customErrors> element via VB.NET Code?


That's C#, the OP wants VB.NET...

;-)



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/
===================================
"Milosz Skalecki [MCAD]" <mily242@DONTLIKESPAMwp.plwrote in message
news:15151AB0-8FA8-48E2-A29B-A47F2C00C971@microsoft.com...
Quote:
Hi Mike,
>
using System.Web.Configuration;
>
// pass application virtual directory name
Configuration configuration =
WebConfigurationManager.OpenWebConfiguration("/TestWebsite");
CustomErrorsSection section =
(CustomErrorsSection)configuration.GetSection("sys tem.web/customErrors");
>
foreach (CustomError error in section.Errors)
{
Response.Write(error.StatusCode);
}
--
Milosz
Quote:
"Mike" wrote:
Quote:
Quote:
>Hi. Is it programatically possible in VB.NET to read the contents of
>web.config's <customErrorselement? I looked at using
>ConfigurationSettings.AppSettings, but that doesn't work.
>>
>I need to read the value of redirect from the error statusCode 404. My
>web.config looks like this:
><customErrors mode="RemoteOnly" defaultRedirect="Error.aspx">
> <error statusCode="404" redirect="Error404.aspx" />
></customErrors>
>>
>Thanks!

Juan T. Llibre
Guest
 
Posts: n/a
#5: Feb 8 '07

re: Read <customErrors> element via VB.NET Code?


I just realized you also wanted the defaultRedirect value.

Here's the modified code :
---------------------------------------
<%@ Page Language="VB" %>

<%@ Import Namespace="System.Web.Configuration" %>
<%@ Import Namespace="System.Configuration" %>

<script runat="server">

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

' Set the root path of the Web application that contains the Web.config file that you want to access.
Dim configuration As System.Configuration.Configuration = WebConfigurationManager.OpenWebConfiguration("/YourAppName")

' Get the section.
Dim customErrorsSection As CustomErrorsSection = CType(configuration.GetSection("system.web/customErrors"), CustomErrorsSection)

' Read the <customErrorssection mode.

Dim currentMode As CustomErrorsMode = customErrorsSection.Mode
Dim currentDefaultRedirect As String = customErrorsSection.defaultRedirect

' Display the <customErrorsand defaultRedirect page information.

ConfigId.Text = "customErrors is: " & currentMode.ToString() & ", and the defaultRedirect page is : " & currentDefaultRedirect

End Sub
</script>

<html >
<head>
<title>Read customErrors Configuration Setting</title>
</head>
<body>

<p>
This page displays the <b>values</bof the <b>customErrors and defaultRedirect</bsections of web.config.
</p>
<h3>Values:</h3>
<p>
<asp:Label ID="ConfigId" BackColor="#dcdcdc" BorderWidth="1" runat="Server" /></p>
</body>
</html>
-------------

The modified sample is at : http://asp.net.do/test/readCustomErrors.aspx



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/
===================================
"Juan T. Llibre" <nomailreplies@nowhere.comwrote in message news:ua7g3m6SHHA.1212@TK2MSFTNGP03.phx.gbl...
re:
Quote:
Hi. Is it programatically possible in VB.NET to read the contents of web.config's <customErrorselement?
Sure it's possible.

See it working at : http://asp.net.do/test/readCustomErrors.aspx

Here's the source for that sample I wrote :

readCustomErrors.aspx:
-----------------------------------

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Web.Configuration" %>
<%@ Import Namespace="System.Configuration" %>

<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

' Set the root path of the Web application that contains the Web.config file that you want to access.

Dim configuration As System.Configuration.Configuration = WebConfigurationManager.OpenWebConfiguration("/test")

' Get the section.

Dim customErrorsSection As CustomErrorsSection = CType(configuration.GetSection("system.web/customErrors"), CustomErrorsSection)

' Read the <customErrorssection mode.

Dim currentMode As CustomErrorsMode = customErrorsSection.Mode

' Display the <customErrorsinformation.

ConfigId.Text = currentMode.ToString()

End Sub
</script>

<html >
<head>
<title>Read customErrors Configuration Setting</title>
</head>
<body>
<p>
This page displays the <b>value</bof the <b>customErrors</bsection of web.config.
</p>
<h3>Value:</h3>
<p>
<asp:Label ID="ConfigId" BackColor="#dcdcdc" BorderWidth="1" runat="Server" /></p>
</body>
</html>
-----------

Enjoy!



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/
===================================
"Mike" <Mike@discussions.microsoft.comwrote in message news:5B4A9B56-8FDA-4045-991F-B87CA3CBE5F3@microsoft.com...
Quote:
Hi. Is it programatically possible in VB.NET to read the contents of
web.config's <customErrorselement? I looked at using
ConfigurationSettings.AppSettings, but that doesn't work.

I need to read the value of redirect from the error statusCode 404. My
web.config looks like this:
<customErrors mode="RemoteOnly" defaultRedirect="Error.aspx">
<error statusCode="404" redirect="Error404.aspx" />
</customErrors>

Thanks!
=?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=
Guest
 
Posts: n/a
#6: Feb 8 '07

re: Read <customErrors> element via VB.NET Code?


I know, he should be fine. Anyway:

imports System.Web.Configuration

'pass application virtual directory name
Dim configuration As Configuration =
WebConfigurationManager.OpenWebConfiguration("/TestWebsite")
Dim section As CustomErrorsSection =
CType(configuration.GetSection("system.web/customErrors"),
CustomErrorsSection)

For Each err As CustomError In section.Errors
Response.Write(err.StatusCode)
Next
--
Milosz


"Juan T. Llibre" wrote:
Quote:
That's C#, the OP wants VB.NET...
>
;-)
>
>
>
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/
===================================
"Milosz Skalecki [MCAD]" <mily242@DONTLIKESPAMwp.plwrote in message
news:15151AB0-8FA8-48E2-A29B-A47F2C00C971@microsoft.com...
Quote:
Hi Mike,

using System.Web.Configuration;

// pass application virtual directory name
Configuration configuration =
WebConfigurationManager.OpenWebConfiguration("/TestWebsite");
CustomErrorsSection section =
(CustomErrorsSection)configuration.GetSection("sys tem.web/customErrors");

foreach (CustomError error in section.Errors)
{
Response.Write(error.StatusCode);
}
--
Milosz
>
>
Quote:
"Mike" wrote:
>
Quote:
Quote:
Hi. Is it programatically possible in VB.NET to read the contents of
web.config's <customErrorselement? I looked at using
ConfigurationSettings.AppSettings, but that doesn't work.
>
I need to read the value of redirect from the error statusCode 404. My
web.config looks like this:
<customErrors mode="RemoteOnly" defaultRedirect="Error.aspx">
<error statusCode="404" redirect="Error404.aspx" />
</customErrors>
>
Thanks!
>
>
>
Juan T. Llibre
Guest
 
Posts: n/a
#7: Feb 8 '07

re: Read <customErrors> element via VB.NET Code?


Hmmm...

I don't program the statusCode choices in web.config.

I program the statusCode errors in the defaultRedirect page:

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="http://asp.net.do/"asp.net.do entry page </a>
</body>
</html>
---------------

In your view, is redirecting to multiple error pages more efficient than
redirecting to a single error page which catches all the possible errors ?

If you think so, why ?




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/
===================================
"Milosz Skalecki [MCAD]" <mily242@DONTLIKESPAMwp.plwrote in message
news:6985FC02-3EE8-4B9D-9B97-BCD9F44E51AB@microsoft.com...
Quote:
>I know, he should be fine. Anyway:
>
imports System.Web.Configuration
>
'pass application virtual directory name
Dim configuration As Configuration =
WebConfigurationManager.OpenWebConfiguration("/TestWebsite")
Dim section As CustomErrorsSection =
CType(configuration.GetSection("system.web/customErrors"),
CustomErrorsSection)
>
For Each err As CustomError In section.Errors
Response.Write(err.StatusCode)
Next
--
Milosz
Quote:
"Juan T. Llibre" wrote:
>
Quote:
>That's C#, the OP wants VB.NET...
>>
>;-)
>>
>>
>>
>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/
>===================================
>"Milosz Skalecki [MCAD]" <mily242@DONTLIKESPAMwp.plwrote in message
>news:15151AB0-8FA8-48E2-A29B-A47F2C00C971@microsoft.com...
Quote:
Hi Mike,
>
using System.Web.Configuration;
>
// pass application virtual directory name
Configuration configuration =
WebConfigurationManager.OpenWebConfiguration("/TestWebsite");
CustomErrorsSection section =
(CustomErrorsSection)configuration.GetSection("sys tem.web/customErrors");
>
foreach (CustomError error in section.Errors)
{
Response.Write(error.StatusCode);
}
--
Milosz
>>
>>
Quote:
"Mike" wrote:
>>
Quote:
>Hi. Is it programatically possible in VB.NET to read the contents of
>web.config's <customErrorselement? I looked at using
>ConfigurationSettings.AppSettings, but that doesn't work.
>>
>I need to read the value of redirect from the error statusCode 404. My
>web.config looks like this:
><customErrors mode="RemoteOnly" defaultRedirect="Error.aspx">
> <error statusCode="404" redirect="Error404.aspx" />
></customErrors>
>>
>Thanks!
>>
>>
>>



Mark Rae
Guest
 
Posts: n/a
#8: Feb 8 '07

re: Read <customErrors> element via VB.NET Code?


"Juan T. Llibre" <nomailreplies@nowhere.comwrote in message
news:uMdOIP8SHHA.4744@TK2MSFTNGP02.phx.gbl...
Quote:
I don't program the statusCode choices in web.config.
>
I program the statusCode errors in the defaultRedirect page:
Me too.


=?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=
Guest
 
Posts: n/a
#9: Feb 8 '07

re: Read <customErrors> element via VB.NET Code?


It was an example how to access each item properties!!! :)
--
Milosz


"Mark Rae" wrote:
Quote:
"Juan T. Llibre" <nomailreplies@nowhere.comwrote in message
news:uMdOIP8SHHA.4744@TK2MSFTNGP02.phx.gbl...
>
Quote:
I don't program the statusCode choices in web.config.

I program the statusCode errors in the defaultRedirect page:
>
Me too.
>
>
>
=?Utf-8?B?TWlrZQ==?=
Guest
 
Posts: n/a
#10: Feb 8 '07

re: Read <customErrors> element via VB.NET Code?


Thank you to everyone who contributed!

Juan, Thank you for providing a perfect example & sample code of what I'm
looking for!

Thanks all!

"Juan T. Llibre" wrote:
Quote:
I just realized you also wanted the defaultRedirect value.
>
Here's the modified code :
---------------------------------------
<%@ Page Language="VB" %>
>
<%@ Import Namespace="System.Web.Configuration" %>
<%@ Import Namespace="System.Configuration" %>
>
<script runat="server">
>
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
>
' Set the root path of the Web application that contains the Web.config file that you want to access.
Dim configuration As System.Configuration.Configuration = WebConfigurationManager.OpenWebConfiguration("/YourAppName")
>
' Get the section.
Dim customErrorsSection As CustomErrorsSection = CType(configuration.GetSection("system.web/customErrors"), CustomErrorsSection)
>
' Read the <customErrorssection mode.
>
Dim currentMode As CustomErrorsMode = customErrorsSection.Mode
Dim currentDefaultRedirect As String = customErrorsSection.defaultRedirect
>
' Display the <customErrorsand defaultRedirect page information.
>
ConfigId.Text = "customErrors is: " & currentMode.ToString() & ", and the defaultRedirect page is : " & currentDefaultRedirect
>
End Sub
</script>
>
<html >
<head>
<title>Read customErrors Configuration Setting</title>
</head>
<body>
>
<p>
This page displays the <b>values</bof the <b>customErrors and defaultRedirect</bsections of web.config.
</p>
<h3>Values:</h3>
<p>
<asp:Label ID="ConfigId" BackColor="#dcdcdc" BorderWidth="1" runat="Server" /></p>
</body>
</html>
-------------
>
The modified sample is at : http://asp.net.do/test/readCustomErrors.aspx
>
>
>
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/
===================================
"Juan T. Llibre" <nomailreplies@nowhere.comwrote in message news:ua7g3m6SHHA.1212@TK2MSFTNGP03.phx.gbl...
re:
Quote:
Hi. Is it programatically possible in VB.NET to read the contents of web.config's <customErrorselement?
>
Sure it's possible.
>
See it working at : http://asp.net.do/test/readCustomErrors.aspx
>
Here's the source for that sample I wrote :
>
readCustomErrors.aspx:
-----------------------------------
>
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Web.Configuration" %>
<%@ Import Namespace="System.Configuration" %>
>
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
>
' Set the root path of the Web application that contains the Web.config file that you want to access.
>
Dim configuration As System.Configuration.Configuration = WebConfigurationManager.OpenWebConfiguration("/test")
>
' Get the section.
>
Dim customErrorsSection As CustomErrorsSection = CType(configuration.GetSection("system.web/customErrors"), CustomErrorsSection)
>
' Read the <customErrorssection mode.
>
Dim currentMode As CustomErrorsMode = customErrorsSection.Mode
>
' Display the <customErrorsinformation.
>
ConfigId.Text = currentMode.ToString()
>
End Sub
</script>
>
<html >
<head>
<title>Read customErrors Configuration Setting</title>
</head>
<body>
<p>
This page displays the <b>value</bof the <b>customErrors</bsection of web.config.
</p>
<h3>Value:</h3>
<p>
<asp:Label ID="ConfigId" BackColor="#dcdcdc" BorderWidth="1" runat="Server" /></p>
</body>
</html>
-----------
>
Enjoy!
>
>
>
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/
===================================
"Mike" <Mike@discussions.microsoft.comwrote in message news:5B4A9B56-8FDA-4045-991F-B87CA3CBE5F3@microsoft.com...
Quote:
Hi. Is it programatically possible in VB.NET to read the contents of
web.config's <customErrorselement? I looked at using
ConfigurationSettings.AppSettings, but that doesn't work.

I need to read the value of redirect from the error statusCode 404. My
web.config looks like this:
<customErrors mode="RemoteOnly" defaultRedirect="Error.aspx">
<error statusCode="404" redirect="Error404.aspx" />
</customErrors>

Thanks!
Closed Thread