gpt4 book ai didi

asp.net - 有没有办法将类似字典的集合用作应用程序设置对象?

转载 作者:行者123 更新时间:2023-12-01 12:43:14 24 4
gpt4 key购买 nike

我想在我的 ASP.NET 网络应用程序的应用程序设置中存储一组键/值对,但我没有找到一种直接的方法来做到这一点。比如这些two questions告诉我 StringDictionary 等不会序列化为 XML,并建议我必须推出自己的实现。但这似乎更容易做到;毕竟,web.config 是 XML,而 本质上是键/值对的集合,所以我感觉好像遗漏了一些明显的东西。鉴于我下面的具体情况,我真的必须推出自己的序列化,还是有更简单的解决方法?

有问题的网络应用程序是一个基本的联系表单,它根据参数的值向不同的收件人发送电子邮件;例如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>

编辑:由于代码着色,用 C# 注释替换了 VB 注释

最佳答案

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

<appSettings>
<add key="EmailSupport" value="support@somedomain.com" />
<add key="EmailSales" value="sales@somedomain.com" />
</appSettings>

然后在你的代码中你只是在做类似的事情:

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

如果你想更整洁一点,你可以创建一个自定义 Configuration Section像这样(恐怕是 C#,但也是 the docs have 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; }
}
}
}

然后在你的 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>

然后你可以调用这个:

emailRecipient = Request["recipient"];

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

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

// send email to recipientEmail.

关于asp.net - 有没有办法将类似字典的集合用作应用程序设置对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/966638/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com