简体   繁体   English

从ASP.NET中的Web.config文件读取值

[英]Reading Values from Web.config file in ASP.NET

I have an ASP.NET MVC 5 app. 我有一个ASP.NET MVC 5应用程序。 My app has a web.config app. 我的应用程序有一个web.config应用程序。 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? 我的问题是,如何在C#代码中访问这些配置值? I'm familiar with ConfigurationManager.AppSettings . 我对ConfigurationManager.AppSettings很熟悉。 However, I'm not sure how to get configuration settings out of custom sections. 但是,我不确定如何从自定义部分中获取配置设置。

You should use ConfigurationManager.GetSection(String) 您应该使用ConfigurationManager.GetSection(String)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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