自定义web.配置部分没有保存

本文关键字:保存 web 配置部 自定义 | 更新日期: 2023-09-27 18:17:56

我在web中实现了自定义部分。配置并希望将数据保存在那里,在运行时调试时,当您检查它包含您放在那里的所有值并保存,没有任何例外。但网络。配置文件没有更新。

我有以下自定义部分在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();

但是网页上没有更新。config,我在这里做错了什么?

自定义web.配置部分没有保存

Try

printXmlConfig.SectionInformation.ForceSave = true;

webConfig.SectionInformation.ForceSave = true;

之前
webConfig.Save(ConfigurationSaveMode.Full);