Managing web.config programmatically

Last post 12-10-2007 3:02 AM by crungcrung. 7 replies.

Sort Posts:

  • Managing web.config programmatically

    02-16-2007, 5:07 AM
    This is totally weird. Why does this code:
    1    System.Configuration.Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~/web.config");
    2    System.Net.Configuration.MailSettingsSectionGroup mailSection = config.GetSectionGroup("system.net/mailSettings") as System.Net.Configuration.MailSettingsSectionGroup;
    3    mailSection.Smtp.From = tbMailFrom.Text;
    4    mailSection.Smtp.Network.Host = tbMailServer.Text;
    5    int mailPort = 25;
    6    int.TryParse(tbMailPort.Text, out mailPort);
    7    mailSection.Smtp.Network.Port = mailPort;
    8    config.Save(ConfigurationSaveMode.Modified);
    throw an exception at line 8:
    System.Configuration.ConfigurationErrorsException: A configuration file cannot be created for the requested Configuration object. at System.Configuration.MgmtConfigurationRecord.SaveAs(String filename, ConfigurationSaveMode saveMode, Boolean forceUpdateAll) at System.Configuration.Configuration.SaveAsImpl(String filename, ConfigurationSaveMode saveMode, Boolean forceSaveAll) at System.Configuration.Configuration.Save(ConfigurationSaveMode saveMode) at Admin_EmailSettings.btnSaveEmailConfig_Click(Object sender, EventArgs e)
     
    I'd love to use the built-in Configuration object offered by NET 2.0. I can read the config without any problem using this object, but I cannot save it!
     
    If I pass null to WebConfigurationManager.OpenWebConfiguration(null), the code above works fine, but instead of updating my web-application's config it updates the file C:\WINNT\Microsoft.NET\Framework\v2.0.50727\CONFIG\web.config
     
    The only solution I have found thus far is to update the web.config treating it as plain xml (and this works!):
    1    System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
    2    doc.Load(Server.MapPath("../web.config"));
    3    string strSel = "/configuration/system.net/mailSettings";
    4    System.Xml.XmlNode nodeMailSettingsOld = doc.SelectSingleNode(strSel);
    5    // remove self
    6    nodeMailSettingsOld.ParentNode.RemoveChild(nodeMailSettingsOld);
    7    System.Xml.XmlNode nodeSystemNet = doc.SelectSingleNode("/configuration/system.net");
    8    System.Xml.XmlNode nodeMailSettings = doc.CreateNode(System.Xml.XmlNodeType.Element, "mailSettings", null);
    9    System.Xml.XmlNode nodeSmtp = doc.CreateNode(System.Xml.XmlNodeType.Element, "smtp", null);
    10   System.Xml.XmlAttribute attFrom = doc.CreateAttribute("from");
    11   attFrom.Value = tbMailFrom.Text;
    12   nodeSmtp.Attributes.Append(attFrom);
    13   nodeMailSettings.AppendChild(nodeSmtp);
    14   nodeSystemNet.AppendChild(nodeMailSettings);
    15   doc.Save(Server.MapPath("../web.config"));
    
     
    I searched these forums to see if anybody encountered similar issues. Some posters reported identical exceptions (in ASP.NET Configuration web-tool) if web-app's path contained characters like &, #, etc. but this is not the case here: ASP.NET Configuration web-tool works fine and the only issue is that I cannot save modified configuration file via the Configuration object.
     
    Any ideas?
    Thank you!
  • Re: Managing web.config programmatically

    02-16-2007, 7:27 AM
    Answer
    Funny as it may sound, but the solution is very simple: OpenWebConfiguration expects virutal path as a parameter, but WITHOUT config's filename!
  • Re: Managing web.config programmatically

    08-17-2007, 10:38 AM
    • Loading...
    • Maharaj
    • Joined on 03-23-2006, 6:25 PM
    • Posts 6

    Hi,

     Another bazzarre thing that I am experiencing is, on my web page we let user configure the smtp host, user name & password. What is happenning is when I load my page I display the values for appsettings, connection string and smtp stuff in the text boxes. Appsettings and connection string pulls up the correct values from the web.config file at the root of the application but smtp info it is looking for the value in the C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG\web.config file & not at the web.config file at the root of the app. I am using the following code to get mailSettings

    System.Net.Configuration.MailSettingsSectionGroup mailSettings = config.GetSectionGroup( AppConstants.MAIL_SETTINGS_GROUP ) as System.Net.Configuration.MailSettingsSectionGroup;

     

    Any in help on these issue will be appreciated

     

    Thanks,

    Salil

     

  • Re: Managing web.config programmatically

    08-17-2007, 12:07 PM

    You are calling config.GetSectionGroup, but you didn't tell what your "config" object is

  • Re: Managing web.config programmatically

    08-17-2007, 4:58 PM
    • Loading...
    • Maharaj
    • Joined on 03-23-2006, 6:25 PM
    • Posts 6

    I get my config object in a method first as follows

    private Configuration GetConfiguration()

    { 

    // Determine current directory.

    String path = Request.CurrentExecutionFilePath;

    path = path.Substring( 0, path.LastIndexOf( "/Admin" ) );

    // Open configuration.

    Configuration config = WebConfigurationManager.OpenWebConfiguration( path );

    return config;

    }

    and then pass it to the method which gets the smtp settings (Mail settings). It is getting me the config correctly, all appsettings except smtp.

    Actually I experimented a little bit further and put mailSettings in my web.config file located in the C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG folder and thats when it started to pull smtp info in the GUI. Which is kind of bazzare. It pulls all other settings from the web.config file in the application folder except mailSettings.

    Any suggestions will be appreciated.

    Thanks,

     Salil

    Filed under:
  • Re: Managing web.config programmatically

    08-18-2007, 5:58 AM

    Well, I have only three suggestions:

    1) You are getting the mailConfiguration by config.GetSectionGroup( AppConstants.MAIL_SETTINGS_GROUP ). Double-check the AppConstants.MAIL_SETTINGS_GROUP, maybe it is not correct. Is it equal to "system.net/mailSettings"?

    2) Double-check your /Admin/'s configuration xml: is the mail section located correctly in it (/configuration/system.net/mailSettings)?

    3) Just in case, try putting/reading mail settings in the config file of your app (the root app's config vs /Admin/'s config). I know it sounds bizzare, but I would test this option.

  • Re: Managing web.config programmatically

    08-18-2007, 9:40 AM
    • Loading...
    • Maharaj
    • Joined on 03-23-2006, 6:25 PM
    • Posts 6

    Thanks for such quick response. I will try your suggestions & see how it goes

    Thanks again

    Salil

  • Re: Managing web.config programmatically

    12-10-2007, 3:02 AM
    • Loading...
    • crungcrung
    • Joined on 09-06-2005, 12:55 PM
    • Manila, Philippines
    • Posts 4

    Maybe this would work better:

    Assuming you have an <appSettings> key of "key1" and value of "value1" and you wish to change the value to "value2".

    Configuration config =  WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
    config.AppSettings.Settings["key1"].Value = "value2";
    config.Save();
     

    Pretty straight forward.  Just make sure that System.Web.Configuration is declared on top of your code.

    Patrick Oliveros
    Web Developer
Page 1 of 1 (8 items)
Microsoft Communities
Page view counter