<?xml version="1.0" encoding="utf-8"?>
<!--
有关如何配置 ASP.NET 应用程序的详细信息,请访问
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<appSettings>
<add key="key1" value="value1"/>
</appSettings>
</configuration>

web.config示例

一、缓存类

 using System;
using System.Collections;
using System.Configuration;
using System.Web;
using System.Web.Caching; namespace Common
{
/// <summary>
/// 缓存帮助类
/// author:陈彦斌
/// 时间:2019年7月14日14:25:30
/// HttpRuntime.Cache
/// </summary>
public sealed class CacheHelper
{
/// <summary>
/// 获取configuratio节点下appSettings中add的值
/// </summary>
/// <param name="key">AppSettings的键</param>
/// <returns></returns>
public static object GetAppSttings(string key)
{
return ConfigurationManager.AppSettings[key];
}
/// <summary>
/// 获取当前应用程序指定CacheKey的值
/// </summary>
/// <param name="CacheKey">appSettings节点下add中的键</param>
/// <returns></returns>
public static object GetCache(string CacheKey)
{
Cache objCache = HttpRuntime.Cache;
return objCache[CacheKey];
}
/// <summary>
/// 设置数据缓存(慎用)
/// </summary>
/// <param name="CacheKey">键</param>
/// <param name="CacheValue">值</param>
public static void SetCache(string CacheKey,object CacheValue)
{
Cache objCache = HttpRuntime.Cache;
objCache.Insert(CacheKey, CacheValue);
}
/// <summary>
/// 设置数据缓存
/// </summary>
/// <param name="CacheKey">键</param>
/// <param name="CacheValue">值</param>
/// <param name="TimeOut">时间间隔</param>
public static void SetCache(string CacheKey, object CacheValue, TimeSpan TimeOut)
{
Cache objCache = HttpRuntime.Cache;
objCache.Insert(CacheKey, CacheValue, null, DateTime.MaxValue, TimeOut, CacheItemPriority.NotRemovable, null);
}
/// <summary>
/// 设置数据缓存
/// </summary>
/// <param name="CacheKey">键</param>
/// <param name="CacheValue">值</param>
/// <param name="absoluteExpiration">绝对过期时间</param>
/// <param name="slidingExpiration">时间间隔</param>
public static void SetCache(string CacheKey, object CacheValue, DateTime absoluteExpiration, TimeSpan slidingExpiration)
{
Cache objCache = HttpRuntime.Cache;
objCache.Insert(CacheKey, CacheValue, null, absoluteExpiration, slidingExpiration);
}
/// <summary>
/// 移除全部缓存
/// </summary>
public static void RemovaAllCache()
{
Cache objCache = HttpRuntime.Cache;
IDictionaryEnumerator CacheEnum = objCache.GetEnumerator();
while (CacheEnum.MoveNext())
{
objCache.Remove(CacheEnum.Key.ToString());
}
}
/// <summary>
/// 移除指定键的缓存
/// </summary>
/// <param name="CacheKey">键</param>
public static void RemovaAllCache(string CacheKey)
{
Cache objCache = HttpRuntime.Cache;
objCache.Remove(CacheKey);
}
}
}

二、AppSetting配置类

 using System;

 namespace Common
{
/// <summary>
/// web.config操作类
/// 使用前需引用程序集:System.configuration
/// </summary>
public sealed class ConfigHelper
{
/// <summary>
/// 获取AppSettings中配置String信息
/// </summary>
/// <param name="key">键</param>
/// <returns></returns>
public static string GetConfigString(string key)
{
object objValue = CacheHelper.GetCache(key);
if (objValue == null) //缓冲区没有值
{
objValue = CacheHelper.GetAppSttings(key);
if (objValue != null)
{
CacheHelper.SetCache(key, objValue, DateTime.Now.AddMinutes(), TimeSpan.Zero);
}
}
return objValue.ToString();
}
/// <summary>
/// 获取AppSettings中配置Bool信息
/// </summary>
/// <param name="key">键</param>
/// <returns></returns>
public static bool GetConfigBool(string key)
{
object objValue= CacheHelper.GetAppSttings(key);
if (StringUtil.isNullOrBlank(objValue))
{
try
{
bool.Parse(objValue.ToString());
return true;
}
catch
{
return false;
}
}
return false;
}
/// <summary>
/// 获取AppSettings中配置decimal信息
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static decimal GetConfigDecimal(string key)
{
object objValue = CacheHelper.GetAppSttings(key);
if (StringUtil.isNullOrBlank(objValue))
{
try
{
return decimal.Parse(objValue.ToString());
}
catch
{
return ;
}
}
return ;
}
/// <summary>
/// 获取AppSettings中配置DateTime信息,可空
/// </summary>
/// <param name="key">键</param>
/// <returns></returns>
public static DateTime? GetConfigDateTime(string key)
{
DateTime? DateTimeNull = null;
object objValue = CacheHelper.GetAppSttings(key);
if (StringUtil.isNullOrBlank(objValue))
{
try
{
return DateTime.Parse(objValue.ToString());
}
catch
{
return DateTimeNull;
}
}
return DateTimeNull;
}
}
}
05-28 23:10