有没有办法使用类似字典的集合作为应用程序设置对象?

Mat*_*att 2 vb.net asp.net dictionary

我想在我的ASP.NET Web应用程序的应用程序设置中存储一组键/值对,但我找不到直接的方法来做到这一点.例如,这两个 问题告诉我,StringDictionary等不会序列化为XML,并建议我必须推出自己的实现.但似乎这应该更容易做到; 毕竟,web.config是XML,<applicationSettings>本质上是键/值对的集合,所以感觉我错过了一些明显的东西.鉴于我的具体情况如下,我是否真的必须推出自己的序列化,还是有更简单的解决方法?

有问题的Web应用程序是一个基本联系表单,它根据参数的值向不同的收件人发送电子邮件; 例如http://www.examplesite.com/Contact.aspx?recipient=support会发送电子邮件至SupportGroup@exampledomain.com.

目标是通过编辑web.config文件来添加或删除收件人(或更改其地址),这样我就不必重新编译,并且可以在测试和生产环境中轻松维护不同的配置.例如:

// I can use something like this for the sender address
SmtpMsg.From = New MailAddress(My.Settings.EmailSender)

// And then just edit this part of web.config to use 
// different addresses in different environments.
<setting name="EmailSender" serializeAs="String">
 <value>webcontact@exampledomain.com</value>
</setting>

// I want something like this for the recipients
SmtpMsg.To.Add(My.Settings.Recipients("support"))

// and presumably some sort of equivalent xml in web.config
// maybe something like this???
<recipients>
  <item name="support" serializeAs="String">
   <value>SupportGroup@exampledomain.com</value>
  </item>
  <!-- add or remove item elements here -->
</recipients>
Run Code Online (Sandbox Code Playgroud)

编辑:由于代码着色而取代了带有C#注释的VB注释

Zha*_*uid 5

简单的方法显然是将它们放在应用程序设置中,但它不会很整洁:

<appSettings>
  <add key="EmailSupport" value="support@somedomain.com" />
  <add key="EmailSales" value="sales@somedomain.com" />
</appSettings>
Run Code Online (Sandbox Code Playgroud)

然后在您的代码中,您只需执行以下操作:

if (!string.IsNullOrEmpty(Request["recipient"])) {
  string recipientEmail = 
         WebConfigurationManager.AppSettings["Email" + Request["recipient"]];
  // Send your email to recipientEmail
}
Run Code Online (Sandbox Code Playgroud)

如果你想要有点整洁,你可以像这样创建一个自定义配置部分(C#我很害怕,但文档也有VB):

namespace EmailSystem {
  public class EmailRecipientsSection : ConfigurationSection {
    [ConfigurationProperty("emailSender", IsRequired = true, IsKey = false)]
    public string EmailSender {
        get { return (string)this["name"]; }
        set { this["name"] = value; }
    }

    [ConfigurationProperty("emailRecipients", IsDefaultCollection = true)]
    public EmailRecipientCollection EmailRecipients {
      get {
        var emailRecipientCollection = 
              (EmailRecipientCollection) base["emailRecipients"];
        return emailRecipientCollection;
      }
    }
  }

  public class EmailRecipientCollection : ConfigurationElementCollection {
    public EmailRecipientElement this[int index] {
      get { return (EmailRecipientElement) BaseGet(index); }
      set {
        if (BaseGet(index) != null) {
          BaseRemoveAt(index);
        }
        BaseAdd(index, value);
      }
    }

    public new EmailRecipientElement this[string name] {
      get { return (EmailRecipientElement) BaseGet(name); }
    }

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

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

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

    [ConfigurationProperty("emailAddress", IsRequired = true)]
    public string EmailAddress {
      get { return (string) this["emailAddress"]; }
      set { this["emailAddress"] = value; }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

然后在你的web.config中有这样的事情:

<configSections>
  [...]
  <section name="EmailSystem" type="EmailSystem, AssmeblyName" />
</configSections>

<EmailSystem emailSender="fromAddress@somedomain.com">
  <emailRecipients>
    <clear />
    <add name="Support" emailAddress="support@somedomain.com" />
    <add name="Sales" emailAddress="sales@somedomain.com" />
  </emailRecipients>
</EmailSystem>
Run Code Online (Sandbox Code Playgroud)

然后你可以调用这个:

emailRecipient = Request["recipient"];

var emailSystem = ConfigurationManager.GetSection("EmailSystem")
                    as EmailRecipientsSection;

string recipientEmail = emailSystem.EmailRecipients[emailRecipient].emailAddress;

// send email to recipientEmail.
Run Code Online (Sandbox Code Playgroud)