Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I Accessing the Settings in Web.config from an HttpModule??? 1

Status
Not open for further replies.

Webwzrd

Programmer
Apr 23, 2002
40
0
0
US
I have writen an custom Error Logging HttpModule and successifully added it to the web.config file. the trouble is that I can not seem to pass variables (Keys) to the module. In this case it is a database connection string.

This is the code I' using in the web.config file:
<configSections>

<section name="ErrorLog" type="ErrorLog.ErrorLog,ErrorLog"/>

</configSections>

<ErrorLog>
<add key="ExtendedReport" value="True" />

<add key="ErrConnectionString" value="My Connection String" />

</ErrorLog>

Here is the code Im using in my HttpModule:

constr=System.Configuration.ConfigurationSettings.AppSettings("ErrConnectionString")


I'm not getting any errors, but I'm not getting any values either.

Any help would be greatly appreciated!!!

Thanks!
Bob


 
Hi there!
Instead of <ErrorLog> put <appSettings> and will work ;)

[morning]
 
In order to use a custom configuration section, you will need to use a NameValueFileSectionHandler. You can either use the one in System.Collections.Sepcialized, or make your own that inhertis from it. It's actually easier to make your own because using system's you have to know the guid and it's a mess.

Here's what I do:

put this in the web.config file:
Code:
[green]define what section groups, sections, and the handler you want to use-->[/green]
<configSections>
		<sectionGroup name="MyCustomSections">
			<section name="SectionOne" type="MyNameSpace.SectionGroupHandler, AssemblyThatContainsTheHandlerClass" />
		</sectionGroup>
</configSections>

[green]<!--put in your custom groups and sections -->[/green]
<MyCustomSections>
    <SectionOne>
        <add key="MySetting" value="MyValue" />
    </SectionOne>
</MyCustomSections>

In your assembly make suer you have a class that inherits from System.Collections.Sepcialized.NameValueFileSectionHandler
Code:
public class SectionGroupHandler : System.Collections.Specialized.NameValueFileSectionHandler{}

Now to access the values...
Code:
NameValueCollection sectionOneStuff = (NameValueCollection)ConfigurationSettings.GetConfig("MyCustomSections/SectionOne");

string mySettingValue = sectionOneStuff["MySetting"];

Hope that helps!

 
Thanks to both of you! Your help was greatly appreciated.

Bob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top