免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
Asp.Net讀取配置文件

為了不使自己寫的代碼看著臃腫,有些固定的值抽取出來,放入配置文件里,這樣,不失為一種好的編碼習(xí)慣。這里介紹兩種不同的文件格式:


一、AppSetting.config(.config格式)


1、首先,配置一下



1 <?xml version="1.0" encoding="utf-8"?>2 <appSettings>3   <!--糾錯(cuò)信息處理狀態(tài)-->4   <add key="CorrectState" value="已處理:0;未處理:1" />5   <!--調(diào)用接口 地址配制-->6   <add key="Api_BaseUrl" value="http://172.16.16.68:8321/"/>7 </appSettings>


2、讀取唯一值



 1         /// <summary> 2         /// 通過key,獲取appSetting的值 3         /// </summary> 4         /// <param name="key">key</param> 5         /// <returns>value</returns> 6         public static string GetWebConfigValueByKey(string key) 7         { 8             string value = string.Empty; 9             Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);10             AppSettingsSection appSetting = (AppSettingsSection)config.GetSection("appSettings");11             if (appSetting.Settings[key] != null)//如果不存在此節(jié)點(diǎn),則添加  12             {13                 value = appSetting.Settings[key].Value;14             }15             config = null;16             return value;17         }





3、讀取多個(gè)值



 1  /// <summary> 2         /// 如果值為 "已審核:1;待審核:0"  形式 ,可通過該函數(shù)獲取該appsetting值的指定value對應(yīng)的key 3         /// </summary> 4         /// <param name="appSettingName"></param> 5         /// <returns></returns> 6         public static string GetKVofKey(string appSettingName, string value) 7         { 8             List<KV> l = new List<KV>(); 9             string str = GetWebConfigValueByKey(appSettingName);10             string[] arr = str.Split(';');11             string rs = "";12             if (arr.Length > 0)13             {14                 int length = arr.Length;15                 for (int i = 0; i < length; i++)16                 {17                     if (arr[i].Split(':')[1] == value)18                     {19                         rs = arr[i].Split(':')[0];20                     }21                 }22             }23             return rs;24         }


所用類



1 1     public class KV2 2     {3 3         public string k;4 4         public string v;5 5     }


二、Api.ini


1、配置文件



1 #微信支付2 [WeiXinPay]3 appId=wx1***14 partnerid=1***15 appSecret=6***5 6 key=N**C


2、ApiHelper.cs(類)



 1 public class ApiHelper 2     { 3         /// <summary> 4         /// 配置文件虛擬路徑 5         /// </summary> 6         private const string ConfigFile = "~/Config/Api.ini"; 7         /// <summary> 8         /// 獲取配置節(jié) 9         /// </summary>10         /// <typeparam name="T">節(jié)點(diǎn)類型(引用類型)</typeparam>11         /// <param name="section">節(jié)點(diǎn)名稱</param>12         /// <returns></returns>13         public static T GetConfigureSection<T>(string section)14             where T:class15         {16             SharpConfig.Configuration configuration = GetIniConfiguration();17             return configuration[section].CreateObject<T>();18         }19         public static string GetPath(string path)20         {21             if (HttpContext.Current !=null)22             {23                 return HttpContext.Current.Server.MapPath(path);24             }25             path = path.Replace("/","\\");26             if (path.StartsWith("\\"))27             {28                 path = path.Substring(path.IndexOf('\\', 1)).TrimStart('\\');29             }30             return Path.Combine(AppDomain.CurrentDomain.BaseDirectory,path);31         }32         /// <summary>33         /// 34         /// </summary>35         /// <returns></returns>36         public static SharpConfig.Configuration GetIniConfiguration() 37         {38             const string SYSTEM_API_CONFIG = "SYSTEM_API_CONFIG";//配置Key39             //如果緩存存在,則提取緩存數(shù)據(jù)40             if (RuntimeCacheHelper.IsExists(SYSTEM_API_CONFIG))41             {42                 return RuntimeCacheHelper.Get<SharpConfig.Configuration>(SYSTEM_API_CONFIG);43             }44                 //從配置文件中加載,并緩存配置45             else46             {47                 SharpConfig.Configuration configuration = null;48                 String strConfigAbsolutePath = GetPath(ConfigFile);49                 configuration = SharpConfig.Configuration.LoadFromFile(strConfigAbsolutePath);50                 //緩存對象51                 RuntimeCacheHelper.Save(SYSTEM_API_CONFIG,configuration,strConfigAbsolutePath,new TimeSpan(365,0,0,0,0));52                 return configuration;53             }54         }55     }


緩存類



 1 public class RuntimeCacheHelper 2     { 3         public static bool IsExists(string Name) 4         { 5             return (HttpRuntime.Cache[Name] != null); 6         } 7         public static T Get<T>(string Name) 8         { 9             if (null != HttpRuntime.Cache[Name]) { return (T)HttpRuntime.Cache[Name]; }10             return default(T);11         }12 13         public static void Save(string Name, object Value, string FileName, TimeSpan expirese)14         {15             var dependency = new CacheDependency(FileName);16             Save(Name,17                 Value,18                 dependency,19                 expirese);20         }21 22         public static void Save(string Name, object Value)23         {24             HttpRuntime.Cache.Insert(Name, Value);25         }26 27         public static void Save(string Name, object Value, CacheDependency Dependency, TimeSpan expirese)28         {29             HttpRuntime.Cache.Insert(Name,30                 Value,31                 Dependency,32                 (DateTime.Now + expirese),33                 System.Web.Caching.Cache.NoSlidingExpiration);34         }35     }



3、請求上下文(HttpContextExtension)類



 1 public static class HttpContextExtension 2     { 3         /// <summary> 4         /// 請求上下文 5         /// </summary> 6         /// <param name="context"></param> 7         /// <returns></returns> 8         public static WeiXinPayConfig GetWeiXinPayConfig(this HttpContextBase context) 9         {10             return ApiHelper.GetConfigureSection<WeiXinPayConfig>("WeiXinPay");11         }12     }


4、獲取值



 1         /// <summary> 2         ///  3         /// </summary> 4         private Models.WeiXinPayConfig wxApiConfig { get; set; } 5         /// <summary> 6         ///  7         /// </summary> 8         /// <returns></returns> 9         public ActionResult GetConfig()10         {11 12             wxApiConfig = HttpContext.GetWeiXinPayConfig();13             14             return View();15         }


本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
C# 動態(tài)修改配置文件 (二)
Asp.net中的web.config配置
SpringBoot集成Jasypt安全框架,配置文件內(nèi)容加密
Ioc依賴注入:Unity4.0.1 在項(xiàng)目中的應(yīng)用 (MVC和API)
.Net之配置文件自定義
C#.NET發(fā)EMAIL的幾種方法
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服