¶Ô±ÈÐÂÎļþ |
| | |
| | | using Microsoft.Extensions.Configuration.Json; |
| | | using Microsoft.Extensions.Configuration; |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.Linq; |
| | | using System.Text; |
| | | using System.Threading.Tasks; |
| | | |
| | | namespace WIDESEA_Core.Helper |
| | | { |
| | | /// <summary> |
| | | /// appsettings.jsonæä½ç±» |
| | | /// </summary> |
| | | public class AppSettings |
| | | { |
| | | public static IConfiguration Configuration { get; set; } |
| | | static string contentPath { get; set; } |
| | | |
| | | public AppSettings(string contentPath) |
| | | { |
| | | string Path = "appsettings.json"; |
| | | |
| | | //å¦æä½ æé
ç½®æä»¶ æ¯ æ ¹æ®ç¯å¢å鿥åå¼äºï¼å¯ä»¥è¿æ ·å |
| | | //Path = $"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json"; |
| | | |
| | | Configuration = new ConfigurationBuilder() |
| | | .SetBasePath(contentPath) |
| | | .Add(new JsonConfigurationSource { Path = Path, Optional = false, ReloadOnChange = true })//è¿æ ·çè¯ï¼å¯ä»¥ç´æ¥è¯»ç®å½éçjsonæä»¶ï¼è䏿¯ bin æä»¶å¤¹ä¸çï¼æä»¥ä¸ç¨ä¿®æ¹å¤å¶å±æ§ |
| | | .Build(); |
| | | } |
| | | |
| | | public AppSettings(IConfiguration configuration) |
| | | { |
| | | Configuration = configuration; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// å°è£
è¦æä½çå符 |
| | | /// </summary> |
| | | /// <param name="sections">èç¹é
ç½®</param> |
| | | /// <returns></returns> |
| | | public static string app(params string[] sections) |
| | | { |
| | | try |
| | | { |
| | | |
| | | if (sections.Any()) |
| | | { |
| | | return Configuration[string.Join(":", sections)]; |
| | | } |
| | | } |
| | | catch (Exception) { } |
| | | |
| | | return ""; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// éå½è·åé
ç½®ä¿¡æ¯æ°ç» |
| | | /// </summary> |
| | | /// <typeparam name="T"></typeparam> |
| | | /// <param name="sections"></param> |
| | | /// <returns></returns> |
| | | public static List<T> app<T>(params string[] sections) |
| | | { |
| | | List<T> list = new List<T>(); |
| | | // å¼ç¨ Microsoft.Extensions.Configuration.Binder å
|
| | | Configuration.Bind(string.Join(":", sections), list); |
| | | return list; |
| | | } |
| | | |
| | | |
| | | /// <summary> |
| | | /// æ ¹æ®è·¯å¾ configuration["App:Name"]; |
| | | /// </summary> |
| | | /// <param name="sectionsPath"></param> |
| | | /// <returns></returns> |
| | | public static string GetValue(string sectionsPath) |
| | | { |
| | | try |
| | | { |
| | | return Configuration[sectionsPath]; |
| | | } |
| | | catch (Exception) { } |
| | | |
| | | return ""; |
| | | |
| | | } |
| | | } |
| | | } |