External AppSettings File NOT merging with web.config

前端 未结 4 2042
长发绾君心
长发绾君心 2021-01-12 00:31

In my app, I have a web.config file with an appSettings section. The appSettings section contains a number of keys that the app uses. The appSettings section also contains

相关标签:
4条回答
  • 2021-01-12 00:42

    I was able to confirm that the external app.config works with a simple project.

    app.Config (in same directory as web.config)

    <appSettings>
      <add key="testAppConfigString" value="APP string exists!"/>
      <add key="testOverrideString" value="!!OVERRIDE string exists in app.config!"/>
    </appSettings>
    

    web.config

    ...
      <appSettings file="app.config">
        <add key="testWebConfigString" value="web config string exists!"/>
        <add key="testOverrideString" value="OVERRIDE string exists in web.config!"/>
      </appSettings>
    ...
    

    Default.aspx

    ...
    <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    
        web: <asp:Label runat="server" ID="lblWeb" /><br/>
        app: <asp:Label runat="server" ID="lblApp" /><br/>
        override: <asp:Label runat="server" ID="lblOverride" /><br/>
    
    </asp:Content>
    ... 
    

    Inside the Default.aspx.cs

        protected void Page_Load(object sender, EventArgs e)
        {
            lblWeb.Text = ConfigurationManager.AppSettings["testWebConfigString"];
            lblApp.Text = ConfigurationManager.AppSettings["testAppConfigString"];
            lblOverride.Text = ConfigurationManager.AppSettings["testOverrideString"];
        }
    

    The resulting page should have the following text:

    web: web config string exists!
    app: APP string exists!
    override: !!OVERRIDE string exists in app.config!
    
    0 讨论(0)
  • 2021-01-12 00:44

    You can access multiple config files by using WebConfigurationmanager method. add namespace:

    using System.Web.Configuration;
    

    So, to access the appSettings of

    ../SomeProjectFolder/Environment/Web.config, you can do:

    var config = WebConfigurationManager.OpenWebConfiguration("~/SomeProjectFolder/Environment/");
    string username = config.AppSettings.Settings["username"].Value;
    

    Hope this helps.

    0 讨论(0)
  • 2021-01-12 00:48

    I just had the same issue (configs not merging as expected), but after explicitly deleting the /bin and /obj directories from the solution-folder and performing a rebuild, everything worked as expected again, so I would suggest you try that and see how it works...

    PS: Also make sure you set the file properties of the external config to 'Copy Always'. Otherwise it won't exist in the bin-directory where your running application lives.

    0 讨论(0)
  • 2021-01-12 00:53

    Perhaps worth mentioning that with Web.config those connectionStrings/@configSource and appSettings/@file are relative to project directory (not target directory). That had me for awhile.

    0 讨论(0)
提交回复
热议问题