简体   繁体   中英

Reading Values from Web.config file in ASP.NET

I have an ASP.NET MVC 5 app. My app has a web.config app. In this app, I have a custom section that looks like this:

<service.settings>
  <service1 isEnabled="true" emailAddress="someone@somewhere.com" />
  <service2 isEnabled="true" emailAddress="another@address.com" />
  <bonzai type="ficus" age="13" />
</service.settings>

My question is, how do I access these configuration values in my C# code? I'm familiar with ConfigurationManager.AppSettings . However, I'm not sure how to get configuration settings out of custom sections.

You should use ConfigurationManager.GetSection(String)

// Assuming 'service.settings' is on the root of your web.config...
var yourSection = (YourSectionClass)ConfigurationManager
   .GetSection("service.settings")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM