自定义web.config部分不保存

时间:2015-10-16 12:18:59

标签: c# asp.net web-config

我已经在web.config中实现了自定义部分,并希望在运行时调试中保存数据,当您检查它包含放在那里的所有值并保存时没有任何异常。但是web.config文件根本没有更新。

我在web.config中有以下自定义部分

 public class PrintersSection : ConfigurationSection
{
    [ConfigurationProperty("PrintForm", Options = ConfigurationPropertyOptions.IsRequired)]
    public PrintFormElement PrintForm
    {
        get
        {
            return (PrintFormElement)this["PrintForm"];
        }
        set { this["PrintForm"] = value; }
    }

    [ConfigurationProperty("Printers", Options = ConfigurationPropertyOptions.IsRequired)]
    public PrintersCollection Printers
    {
        get
        {
            return (PrintersCollection)this["Printers"];
        }
        set { this["Printers"] = value; }
    }

    public override bool IsReadOnly()
    {
        return false;
    }
}



public class PrintFormElement : ConfigurationElement
{
    [ConfigurationProperty("xmlPath")]
    public string XmlPath
    {
        get
        {
            return (string)base["xmlPath"];
        }
        set { base["xmlPath"] = value; }
    }

    [ConfigurationProperty("formName")]
    public string FormName
    {
        get
        {
            return (string)base["formName"];
        }
        set { base["formName"] = value; }
    }

    [ConfigurationProperty("sdateFormat")]
    public string SDateFormat
    {
        get
        {
            return (string)base["sdateFormat"];
        }
        set { base["sdateFormat"] = value; }
    }

    [ConfigurationProperty("createOnTechpadMarkAsComplete")]
    public bool CreateOnTechpadMarkAsComplete
    {
        get
        {
            return (bool)base["createOnTechpadMarkAsComplete"];
        }
        set { base["createOnTechpadMarkAsComplete"] = value; }
    }

    [ConfigurationProperty("createOnSurveySubmit")]
    public bool CreateOnSurveySubmit
    {
        get
        {
            return (bool)base["createOnSurveySubmit"];
        }
        set { base["createOnSurveySubmit"] = value; }
    }

    public override bool IsReadOnly()
    {
        return false;
    }
}


 [ConfigurationCollection(typeof(PrinterElement), AddItemName = "printer", CollectionType = ConfigurationElementCollectionType.AddRemoveClearMap)]
public class PrintersCollection : ConfigurationElementCollection
{
    protected override ConfigurationElement CreateNewElement()
    {
        return new PrinterElement();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        if (element == null)
            throw new ArgumentNullException("element");

        return ((PrinterElement)element).Location;
    }

    [ConfigurationProperty("default", IsRequired = false)]
    public string Default
    {
        get
        {
            return (string)base["default"];
        }
        set { base["default"] = value; }
    }

    public new PrinterElement this[string location]
    {
        get { return base.BaseGet(location) as PrinterElement; }
    }

    public void Add(PrinterElement element)
    {
        base.BaseAdd(element);
    }

    public void Clear()
    {
        base.BaseClear();
    }

    public override bool IsReadOnly()
    {
        return false;
    }
}


public class PrinterElement : ConfigurationElement
{
    [ConfigurationProperty("location", IsRequired = true, IsKey = true)]
    public string Location
    {
        get
        {
            return (string)base["location"];
        }
        set { base["location"] = value; }
    }

    [ConfigurationProperty("name", IsRequired = true)]
    public string Name
    {
        get
        {
            return (string)base["name"];
        }
        set { base["name"] = value; }
    }

    public override bool IsReadOnly()
    {
        return false;
    }
}

它在web.config中的外观如何:

<printers>
  <PrintForm xmlPath="H:\Temp\print3.txt" formName="test-form"
    sdateFormat="MM-dd-yyyy" createOnTechpadMarkAsComplete="true"
    createOnSurveySubmit="true" />
  <Printers default="PrinterA">
    <clear />
    <printer location="1" name="PrinterA" />
    <printer location="2" name="PrinterB" />
  </Printers>
</printers>

这是我编辑它的方式:

 webConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");

            var printXmlConfig = (PrintersSection)ConfigurationManager.GetSection("printers");

            printXmlConfig.PrintForm.XmlPath = model.PrintXMLPath;
            printXmlConfig.PrintForm.FormName = model.PrintXMLFormName;
            printXmlConfig.Printers.Default = model.PrintXMLPrinterName;
            printXmlConfig.PrintForm.SDateFormat = model.PrintXMLSDateFormat;
            printXmlConfig.PrintForm.CreateOnTechpadMarkAsComplete = model.PrintXMLCreateOnTechpadMarkAsComplete;
            printXmlConfig.PrintForm.CreateOnSurveySubmit = model.PrintXMLCreateOnSurveySubmit;
            printXmlConfig.Printers.Clear();
            model.PrinterPerLocation.ForEach(p => printXmlConfig.Printers.Add(new PrinterElement { Location = p.Key, Name = p.Value}));

            webConfig.Save();

但是web.config中没有任何更新,我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

尝试

printXmlConfig.SectionInformation.ForceSave = true;

webConfig.SectionInformation.ForceSave = true;

之前

webConfig.Save(ConfigurationSaveMode.Full);