English 中文(简体)
AppSettings on a different .config file not being updated
原标题:

I am trying to do the exact same thing mentioned on this recent previous question. In essence, here s the case (which is exactly my same situation):

My plan is to have these (appSettings) in their own file (Settings.config), to which I will grant modify permissions to the web process user account, and store all editable settings in this file (e.g. forum title, description, etc).

The problem is that the solution accepted in that question doesn t work for me because instead of saving the appSettings in the separate .config file, when I issue the config.Save(ConfigurationSaveMode.Minimal, false) command, it replicates all the appSettings of the separate file into the appSettings section of the main web.config file (with the new changes). Here s my final code (in vb.net):

Public Shared Function GetAppSetting(ByVal setting As String) As String
    Dim config As System.Configuration.Configuration = _
            WebConfigurationManager.OpenWebConfiguration("~")

    Return config.AppSettings.Settings(setting).Value
End Function

Public Shared Sub SetAppSetting(ByVal setting As String, ByVal value As String)
    Dim config As System.Configuration.Configuration = _
            WebConfigurationManager.OpenWebConfiguration("~")

    config.AppSettings.Settings(setting).Value = value

    config.Save(ConfigurationSaveMode.Minimal, False)
    ConfigurationManager.RefreshSection("appSettings")
End Sub

Basically I can t see where I would be indicating that I want the settings to be saved on the separate file instead of on the web.config which is where they are stored by default. Oh and by the way, I had to add the file= attribute on the appSettings section of the web.config so that the Settings.config appSettings would be actually taken into account. Without that attribute the above code doesn t read the separate .config file settings. Here s a snapshot of my web.config appSettings section:

  <appSettings file="Settings.config">
    <add key="RestartApp" value="-1" />
  </appSettings>

And here s the entire contents of my Settings.config file:

  <appSettings>
    <add key="AppTitle" value="MVC Web Access" />
    <add key="DefaultWebpage" />
    <add key="CustomCSS" />
    <add key="TktsEmailTo" value="email@email.com" />
    <add key="EmailFrom" value="email@email.com" />
    <add key="EmailFromSMTP" value="mail.email.com" />
    <add key="EmailFromPW" value="fakePassword" />
  </appSettings>

So instead of ending up with modified settings on my Settings.config file after the .save command, my appSettings section on the web.config file ends up like this (and the Settings.config file remains untouched):

  <appSettings file="Settings.config">
    <add key="RestartApp" value="-1" />
    <add key="AppTitle" value="New title" />
    <add key="DefaultWebpage" value="index.aspx" />
    <add key="CustomCSS" />
    <add key="TktsEmailTo" value="newemail@email.com" />
    <add key="EmailFrom" value="newemail@email.com" />
    <add key="EmailFromSMTP" value="mail.email.com" />
    <add key="EmailFromPW" value="NewFakePassword" />
  </appSettings>
问题回答

Just double-checked - the only difference I can see is that I m using

<appSettings configSource="Settings.config"/>

Here is the code I m using now, which is working and saving settings to my separate settings file (Settings.config):

var config = WebConfigurationManager.OpenWebConfiguration("~");

foreach (var key in collection.Keys)
{
    if (config.AppSettings.Settings[key.ToString()] != null)
    {
        config.AppSettings.Settings[key.ToString()].Value = collection[key.ToString()];
    }
}

config.Save(ConfigurationSaveMode.Minimal, false);
ConfigurationManager.RefreshSection("appSettings");

What happens if you use configSource on your appSettings key?





相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签