简体   繁体   English

如何从多个位置标签中有多个appSettings标签的配置文件中读取appSettings?

[英]How to read appSettings from a config file where there are multiple appSettings tag inside multiple location tag?

I am suppose to retrieve the appSettings key to read in a text file. 我想检索appSettings键以读取文本文件。 now the problem is in my web.config file , there are multiple location tags and inside them we have multiple appSettings tag.Hence my code is failing to target the location tag appSettings which i actually need. 现在问题出在我的web.config文件中,里面有多个位置标签,里面有多个appSettings标签,因此我的代码无法定位我实际需要的位置标签appSettings。

  <location path="BidOnline">
    <appSettings/>
    <system.web>
      <authorization>
        <allow users="*"/>
        <!-- Allow all users to the public location -->
      </authorization>
    </system.web>
  </location>
  <location path="Profdev/Public">
    <appSettings/>
    <system.web>
      <authorization>
        <allow users="*"/>
        <!-- Allow all users to the public location -->
      </authorization>
    </system.web>
  </location>

  <location path="Punchout">
    <system.webServer>
      <httpProtocol>
        <customHeaders>
          <clear />
          <add name="X-XSS-Protection" value="0"/>
        </customHeaders> 
      </httpProtocol>
    </system.webServer>
  </location>
  <!-- FACTS Service -->
  <location path="Services/Facts">
    <appSettings>
      <!-- Disabeling the security should ONLY be done in test mode. -->
      <add key="DisableSecurity" value="false"/>
      <!-- 
        The ip addresses listed below are the only address that can
        be used while security is enabled (by default.)  Any other
        ip address attempting to post to the service will not be 
        able to.
      -->
      <add key="IPAddresses" value="63.84.95.4;162.40.107.4;207.5.114.131;216.69.96.174"/>
      <!-- Set the ledger that the ar_text_mstr/dtl records will be setup with. -->
      <add key="Ledger" value="GL"/>
    </appSettings>

The location tag contains values that override other web.config settings when the path attribute contains the requested page. path属性包含请求的页面时, location标记包含的值将覆盖其他web.config设置。 In most scenarios we don't need to put appSettings inside of it. 在大多数情况下,我们不需要将appSettings放入其中。 It's better to put all settings in the top-level appSettings element and only put them in a location element if we need to override them for pages within a particular path. 最好将所有设置放在顶层appSettings元素中,仅在需要覆盖特定路径中的页面的设置时,才将它们放在location元素中。

In this case there is only one appSettings element with values, and it's inside this tag: 在这种情况下,只有一个带有值的appSettings元素,它位于此标记内:

<location path="Services/Facts">
    <appSettings>
         <add key="DisableSecurity" value="false"/>
         <!-- other settings -->
    </appSettings>
</location>

That means that those settings only "exist" if the requested page is in the /services/facts folder. 这意味着只有在请求的页面位于/ services / facts文件夹中时,这些设置才“存在”。

If you need an appSettings element that's available to any page, just create one "up a level" in the configuration element: 如果您需要任何页面都可用的appSettings元素,只需在configuration元素中创建一个“上一级”:

<configuration>
    <appSettings>
         <add key="YourSetting" value="false"/>
         <!-- other settings -->
    </appSettings>
</configuration>

Now it will always be accessible regardless of what page (or other part of the application) you're in. 现在,无论您位于哪个页面(或应用程序的其他部分),都将始终可以访问它。


I don't see this used much with appSettings , although that might just be me. 我认为appSettings并没有太多用,尽管那可能只是我。 I can see some room for confusion because many developers will assume that if there is a "root" appSettings section, those values will never, ever change while the application is running, and they might store them in a string for some reason (although there's usually no reason to do that.) They might not realize that there are other appSettings inside location tags, and that the first value they read might get reused for other pages in different paths. 我可以看到一些混淆的空间,因为许多开发人员会假设,如果有一个“ root” appSettings部分,则在应用程序运行时,这些值将永远不会改变,并且出于某些原因,它们可能以字符串形式存储(尽管通常他们没有理由这样做。)他们可能没有意识到location标记中还有其他appSettings ,而且读取的第一个值可能会被其他路径中的其他页面重用。 Or someone could add a location\\appSettings later and it wouldn't work. 或者有人可以稍后添加location\\appSettings ,它将不起作用。

I guess the conclusion that leads me to is not that location\\appSettings is bad, but that we should always make sure we're reading settings on every request and not storing them in a variable that gets reused between requests. 我想得出的结论不是location\\appSettings不好,而是我们应该始终确保在每个请求上读取设置,而不是将它们存储在在请求之间重用的变量中。


Technically most of the appSettings sections shown in your web.config don't matter, because they're empty. 从技术上讲,web.config中显示的大部分appSettings部分都没有关系,因为它们为空。

<appSettings/>

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

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