0

我计划为 FormFields、QueryString 参数等设置配置键。在我的 web.config 中,我有如下设置:

<WhiteListPaametersGroup>
  <WhiteListPaameters>
    <FormField1>EVENTVALIDATION</FormField1>
    <FormField2>VIEWSTATE</FormField2>
    <FormField3>Button1</FormField3>

    <QueryString1>firstname</QueryString1>
    <QueryString2>lastname</QueryString2>

  </WhiteListPaameters>
</WhiteListPaametersGroup>

然后在我的代码中,我读取的值如下:

Dictionary<string, string> parameters = new Dictionary<string,string>();

foreach (XmlNode n in section.ChildNodes)
{
    parameters.Add(n.Name, n.InnerText);
}

有没有更好的存储方法。稍后,我希望能够像对象一样通过字典,并能够获取 FormFields、Querystrings 等的设置。

如果我能写得更干净,请告诉我。

谢谢

4

3 回答 3

1

您可以使用 XML 序列化来存储您的设置并将它们直接恢复到一个对象中。它并不完美,但很容易设置并为您提供对象保存/恢复。

有这个类(属性必须是公共的):

public class WhiteListParameters
{
    public string FormField1 { get; set; }
    public string FormField2 { get; set; }
    public string FormField3 { get; set; }

    public string QueryString1 { get; set; }
    public string QueryString2 { get; set; }
}

要将其保存到 XML 文件,请运行以下代码:

WhiteListParameters parms = new WhiteListParameters
                                {
                                    FormField1 = "EVENTVALIDATION",
                                    FormField2 = "VIEWSTATE",
                                    FormField3 = "Button1",
                                    QueryString1 = "firstname",
                                    QueryString2 = "lastname"
                                };

using(StreamWriter sw = new StreamWriter("C:\\temp\\config.xml"))
{
    XmlSerializer xs = new XmlSerializer(typeof (WhiteListParameters));
    xs.Serialize(sw, parms);
    sw.Close();
}

要将其读回对象:

using(StreamReader sr = new StreamReader("c:\\temp\\config.xml"))
{
    XmlSerializer xs = new XmlSerializer(typeof (WhiteListParameters));
    WhiteListParameters parms = (WhiteListParameters) xs.Deserialize(sr);
    sr.Close();
}
于 2012-04-20T22:38:08.857 回答
0

您可能会考虑的一个选项是创建一个自定义配置部分,您可以在web.config.

这是我为引用我们的 XSLT 模板而创建的配置部分:

namespace Foo.Web.Applications.CustomConfigurationSections
{
    public class XslTemplateConfiguration : ConfigurationSection
    {
        [ConfigurationProperty("xslTemplates")]
        public XslTemplateElementCollection XslTemplates
        {
            get { return this["xslTemplates"] as XslTemplateElementCollection; }
        }
    }

    public class XslTemplateElement : ConfigurationElement
    {
        [ConfigurationProperty("name", IsRequired = true, IsKey = true)]
        public string Name
        {
            get { return this["name"] as string; }
            set { this["name"] = value; }
        }

        [ConfigurationProperty("path", IsRequired = true)]
        public string Path
        {
            get { return this["path"] as string; }
            set { this["path"] = value; }
        }
    }

    public class XslTemplateElementCollection : ConfigurationElementCollection
    {
        public XslTemplateElement this[object key]
        {
            get { return base.BaseGet(key) as XslTemplateElement; }
        }

        public override ConfigurationElementCollectionType CollectionType
        {
            get { return ConfigurationElementCollectionType.BasicMap; }
        }

        protected override string ElementName
        {
            get { return "xslTemplate"; }
        }

        protected override bool IsElementName(string elementName)
        {
            bool isName = false;
            if (!String.IsNullOrEmpty(elementName))
                isName = elementName.Equals("xslTemplate");
            return isName;
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new XslTemplateElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((XslTemplateElement)element).Name;
        }
    }
}

您可以在 web.config 中注册此部分,如下所示:

<configSections>
    <section name="xslTemplateConfiguration" type="Foo.Web.Applications.CustomConfigurationSections.XslTemplateConfiguration"/>
</configSections>

您可以像这样访问代码中的集合:

var config = WebConfigurationManager.OpenWebConfiguration("/");
if (config.HasFile)
{
    var templates = config.GetSection("xslTemplateConfiguration") as XslTemplateConfiguration;
    if (templates != null)
    {
        var templatePath = templates.XslTemplates["PO"].Path;
    }
}
于 2012-04-20T23:23:56.130 回答
0

对于读取配置,.NET Framework 在System.Configuration命名空间中有许多类。最重要的类是ConfigurationSectionConfigurationElement从这些类派生,添加所需的属性并使用ConfigurationProperty属性装饰它们。这种方法的优点是类型安全、可以定义有效列表以及事实,即 .NET Framework 框架将自动为您完成所有来自 web.config 的值的读取、解析和检查。

于 2012-04-20T23:24:32.030 回答