我们经常存储一些配制信息在AppSettings中,是不是经常要写取它们值的代码。让我们写一个简单的基类吧:
1: /// <summary>
2: /// BaseWebConfig
3: /// </summary>
4: public class BaseWebConfig
5: {
6: #region Methods (6)
7:
8: // Protected Methods (6)
9:
10: /// <summary>
11: /// Gets the app setting.
12: /// </summary>
13: /// <param name="appSettingName">Name of the app setting.</param>
14: /// <returns></returns>
15: protected static string GetAppSetting(string appSettingName)
16: {
17: if (ConfigurationManager.AppSettings[appSettingName] == null)
18: throw new NullReferenceException(appSettingName +
19: " AppSettings value cannot be null or empty in the web.config.");
20: return ConfigurationManager.AppSettings[appSettingName];
21: }
22:
23: /// <summary>
24: /// Gets the bool app setting.
25: /// </summary>
26: /// <param name="appSettingName">Name of the app setting.</param>
27: /// <returns></returns>
28: protected static bool GetBoolAppSetting(string appSettingName)
29: {
30: return Convert.ToBoolean(GetAppSetting(appSettingName));
31: }
32:
33: /// <summary>
34: /// Gets the connection string.
35: /// </summary>
36: /// <param name="connectionStringName">Name of the connection string.</param>
37: /// <returns></returns>
38: protected static string GetConnectionString(string connectionStringName)
39: {
40: if (ConfigurationManager.ConnectionStrings[connectionStringName] == null)
41: throw new NullReferenceException(connectionStringName +
42: " ConnectionStrings value cannot be null in the web.config.");
43: if (String.IsNullOrEmpty(ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString))
44: throw new NullReferenceException(connectionStringName +
45: " ConnectionStrings value cannot be null or empty in the web.config.");
46: return ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
47: }
48:
49: /// <summary>
50: /// Gets the date time app setting.
51: /// </summary>
52: /// <param name="appSettingName">Name of the app setting.</param>
53: /// <returns></returns>
54: protected static DateTime GetDateTimeAppSetting(string appSettingName)
55: {
56: DateTime dt;
57: if (!DateTime.TryParse(GetAppSetting(appSettingName), out dt))
58: throw new InvalidCastException(appSettingName + " AppSettings value is an invalid DateTime.");
59: return dt;
60: }
61:
62: /// <summary>
63: /// Gets the int app setting.
64: /// </summary>
65: /// <param name="appSettingName">Name of the app setting.</param>
66: /// <returns></returns>
67: protected static int GetIntAppSetting(string appSettingName)
68: {
69: int i;
70: if (!int.TryParse(GetAppSetting(appSettingName), out i))
71: throw new InvalidCastException(appSettingName + " AppSettings value is an invalid integer.");
72: return i;
73: }
74:
75: /// <summary>
76: /// Gets the string array setting.
77: /// </summary>
78: /// <param name="appSettingName">Name of the app setting.</param>
79: /// <returns></returns>
80: protected static string[] GetStringArraySetting(string appSettingName)
81: {
82: string[] values = GetAppSetting(appSettingName).Split(new[] {','});
83: return values;
84: }
85:
86: #endregion Methods
87: }
然后如何用呢?看下面的代码:
1: public class WebConfig1 : BaseWebConfig
2: {
3: public static string ReturnEmail
4: {
5: get { return GetAppSetting("ReturnEmail"); }
6: }
7:
8: public static bool IsTestEnvironment
9: {
10: get { return GetBoolAppSetting("IsTestEnvironment"); }
11: }
12:
13: public static DateTime ShutOffDate
14: {
15: get { return GetDateTimeAppSetting("ShutOffDate"); }
16: }
17:
18: public static string DbConnectionString
19: {
20: get { return GetConnectionString("DbConnectionString"); }
21: }
22: }
是不是很简单呀。这个代码你可以放到你自己的类库了,更重要是思想,重用的思想。DRY。Don’t repeat yourself.
希望这篇POST对您有帮助。
Author :Petter Liu http://wintersun.cnblogs.com
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· spring官宣接入deepseek,真的太香了~