前段时间做一个收发邮件的模块,收件人邮箱要配置一下为了方便后期更改就把收件人邮箱配置在web.config里边在此记录一下。
添加一个帮助类,直接搜可以找的到
using System;
using System.Configuration;
using System.Globalization;
namespace Common
{
public static class Util
{
#region Public Methods
/// <summary>
/// Gets configuration value for the specified key (null if key does not exist)
/// </summary>
/// <param name="key">Configuration key</param>
/// <returns></returns>
public static string GetConfigValue(
string key
)
{
return ConfigurationManager.AppSettings.Get(key);
}
public static string GetConnectionString(
string name
)
{
return ConfigurationManager.ConnectionStrings[name].ConnectionString;
}
/// <summary>
/// Gets configuration value for the specified key (0 if key does not exist)
/// </summary>
/// <param name="key">Configuration key</param>
/// <returns></returns>
public static int GetConfigIntValue(
string key
)
{
string value = GetConfigValue(key);
int intValue;
int.TryParse(value,
NumberStyles.Integer,
CultureInfo.InvariantCulture,
out intValue);
return intValue;
}
/// <summary>
/// Gets configuration value for the specified key (uses default value if key does not exist)
/// </summary>
/// <param name="key">Configuration key</param>
/// <param name="defaultValue">Default value if key does not exist</param>
/// <returns></returns>
public static string GetConfigValue(
string key,
string defaultValue
)
{
string value = GetConfigValue(key);
if (value == null)
{
value = defaultValue;
}
return value;
}
/// <summary>
/// Gets configuration value for the specified key (uses default value if key does not exist)
/// </summary>
/// <param name="key">Configuration key</param>
/// <param name="defaultValue">Default value if key does not exist</param>
/// <returns></returns>
public static int GetConfigIntValue(
string key,
int defaultValue
)
{
string value = GetConfigValue(key);
int intValue;
if (!int.TryParse(value,
NumberStyles.Integer,
CultureInfo.InvariantCulture,
out intValue))
{
intValue = defaultValue;
}
return intValue;
}
/// <summary>
/// Gets configuration value for the specified key (uses default value if key does not exist)
/// </summary>
/// <param name="key">Configuration key</param>
/// <param name="defaultValue">Default value if key does not exist</param>
/// <returns></returns>
public static bool GetConfigBooleanValue(
string key,
bool defaultValue
)
{
string value = GetConfigValue(key);
if (string.IsNullOrEmpty(value))
{
return defaultValue;
}
return value.Trim().Equals("true", StringComparison.OrdinalIgnoreCase) ||
value.Trim().Equals("yes", StringComparison.OrdinalIgnoreCase);
}
/// <summary>
/// Updates a specific app setting in the config
/// </summary>
/// <param name="name"></param>
/// <param name="value"></param>
/// <returns></returns>
/// I noticed that nothing seemed to be written to the configuration file while debugging within Visual Studio,
/// but once I published the application it worked as expected.
/// it’s because when you make that call vs is editing the vshost.exe.config file in the debug directory not the app.config in your project.
public static bool UpdateAppSetting(string name, string value)
{
try
{
Configuration oConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
oConfig.AppSettings.Settings[name].Value = value;
oConfig.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
return true;
}
catch (Exception)
{
return false;
}
}
public static bool IsTest { get { return true; } }
#endregion
}
}
在web.config里边
<appSettings>
<!-- 密送邮件-->
<add key="MailBCC" value="2669420437@qq.com" />
<!-- 抄送邮件-->
<add key="MailCC" value="2669420437@qq.com" />
<!-- 报错邮件收件人-->
<add key="Mailmassage" value="2669420437@qq.com" />
</appSettings>
在控制器里边
MailCC = Util.GetConfigValue("MailCC");
MailBCC = Util.GetConfigValue("MailBCC");
这样改邮箱地址直接改web.config里边的地址就可以了
除了配置邮箱配置其他的东西也可以这样获取。