473,288 Members | 1,693 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,288 software developers and data experts.

Read <customErrors> element via VB.NET Code?

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!
Feb 8 '07 #1
9 4250
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:
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!
Feb 8 '07 #2
re:
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" <Mi**@discussions.microsoft.comwrote in message news:5B**********************************@microsof t.com...
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!
Feb 8 '07 #3
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]" <mi*****@DONTLIKESPAMwp.plwrote in message
news:15**********************************@microsof t.com...
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:
>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!

Feb 8 '07 #4
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" <no***********@nowhere.comwrote in message news:ua**************@TK2MSFTNGP03.phx.gbl...
re:
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" <Mi**@discussions.microsoft.comwrote in message news:5B**********************************@microsof t.com...
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!
Feb 8 '07 #5
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:
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]" <mi*****@DONTLIKESPAMwp.plwrote in message
news:15**********************************@microsof t.com...
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:
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!


Feb 8 '07 #6
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]" <mi*****@DONTLIKESPAMwp.plwrote in message
news:69**********************************@microsof t.com...
>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:
>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]" <mi*****@DONTLIKESPAMwp.plwrote in message
news:15**********************************@microso ft.com...
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:
>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!



Feb 8 '07 #7
"Juan T. Llibre" <no***********@nowhere.comwrote in message
news:uM**************@TK2MSFTNGP02.phx.gbl...
I don't program the statusCode choices in web.config.

I program the statusCode errors in the defaultRedirect page:
Me too.
Feb 8 '07 #8
It was an example how to access each item properties!!! :)
--
Milosz
"Mark Rae" wrote:
"Juan T. Llibre" <no***********@nowhere.comwrote in message
news:uM**************@TK2MSFTNGP02.phx.gbl...
I don't program the statusCode choices in web.config.

I program the statusCode errors in the defaultRedirect page:

Me too.
Feb 8 '07 #9
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:
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" <no***********@nowhere.comwrote in message news:ua**************@TK2MSFTNGP03.phx.gbl...
re:
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" <Mi**@discussions.microsoft.comwrote in message news:5B**********************************@microsof t.com...
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!
Feb 8 '07 #10

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

Similar topics

2
by: Maureen | last post by:
Hi: Our ASP.NET webapplication works on our development webserver, but fails on our production webserver - I cannot get any detailed error information: -It is an application -Mode in...
1
by: Menno Abbink | last post by:
Hey All, I'm developing a new website and I want the customer to be able to se my work in progress so I have set up a server behind my ADSL-router. The server is using W2003. I've created a...
1
by: Savas Ates | last post by:
I have a web application It works well in my local fodler.. When i upload it to my web server i got To enable the details of this specific error message to be viewable on remote machines, please...
5
by: Savas Ates | last post by:
I have a web application It works well in my local folder.. When i upload it to my web server i got To enable the details of this specific error message to be viewable on remote machines,...
1
by: VB Programmer | last post by:
My 2.0 webapp works great. I added a profile section and all of a sudden I get a slew of "Could not find schema information for the element...." errors/messages. Any ideas why? Here's part of...
1
by: Jayyde | last post by:
New to this so please bear with me... I set up the: <customErrors mode="On" defaultRedirect="~/Unavailable.aspx"> </customErrors> And what it'll do is redirect to the Unavailable.aspx...
3
by: =?Utf-8?B?bmt3?= | last post by:
I want to use <customErrors mode="Off"/in the web.config file; but I want to have custom error message for database time out error message. Is it possible?
1
by: viveknaik17 | last post by:
When i upload aspx pages, it gives error of type.. Details: To enable the details of this specific error message to be viewable on remote machines, please create a <customErrors> tag within a...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.