1

Inside my web.config file I've got code like this:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    ...
    <section name="UninstallSurveySettings" type="dashboard.UninstallSurveyConfig" />
  </configSections>
  ...
  <UninstallSurveySettings>
    <add key="fileLocation" value="C:\inetpub\wwwroot\output\" />
  </UninstallSurveySettings>
   ...
</configuration>

I need to be able to access this field from my custom control. The control can be dropped into any website and needs to check that site's web.config for the fileLocation value in UninstallSurveySetting.

I've tried a couple different approaches with no luck. Any help on this would be greatly appreciated.

4
  • What have you tried and why didn't it work? This should be no different to accessing configuration from a page or any other part of a ASP.Net application. Commented Jul 20, 2011 at 15:59
  • 1
    Instead of making your control depend on a configuration section, pass in a configuration object to it - it will make the control more testable as well as less coupled, and make accessing the configuration the responsibility of the calling code. Commented Jul 20, 2011 at 16:01
  • Please show some code on how you are trying to access this custom section of the web.config. Commented Jul 20, 2011 at 16:06
  • 1
    Why do you use .NET 1.x in 2010?! Commented Jul 20, 2011 at 16:59

4 Answers 4

3

Much easier to use AppSettings.

Web.config:

<configuration>    
    <appSettings>
        <add key="fileLocation" value="C:\inetpub\wwwroot\output\" />
    </appSettings>    
</configuration>

Code:

string location = System.Configuration.ConfigurationManager.AppSettings["fileLocation"];

If your section will become more complex, then:

var section = (NameValueFileSectionHandler)ConfigurationManager.GetSection("UninstallSurveySettings");
if (section != null)
{
    // access section members
}

P.S.

Maybe you want to use ConfigurationSection class instead of handler.

Sign up to request clarification or add additional context in comments.

3 Comments

I would prefer to use the section code since I'm actually inputting data into a much bigger web.config file and I don't want to run the chance of conflicting name values. Assuming the section isn't null, how do I access the value of the fileLocation field?
@Dexter: Please post your section implementation.
@Dexter: Do you have any C# code? How do you declare you section in code? Do you inherit from NameValueFileSectionHandler?
0

In ASP.NET MVC 3 the tag cannot be a direct child of (it results in a configuration error).

How about adding your key to the section. Then you can easily access it via the ConfigurationManager.AppSettings collection.

Comments

0

using System.Configuration.ConfigurationManager and you will be able to get what you want from the web.config

Comments

0

I was able to solve this by creating a configuration class for it and placing this code in the web.config:

<section name="UninstallSurveyConfig" type="dashboard.UninstallSurveyConfig" />
..

<UninstallSurveyConfig dirFileLocation="C:\inetpub\wwwroot\build\output" webFileLocation="~/output" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.