Asp.Net Core 進階(一) —— 讀取appsettings.json

  咱們之前在Asp.Net MVC中使用 System.Configuration.ConfigurationManager 來讀取web.config文件。可是Asp.Net Core MVC已經沒有web.config文件了,它的配置信息通常寫在appsettings.json當中,那麼咱們怎麼讀取該文件呢?web

  在Asp.Net Core MVC中使用 Microsoft.Extensions.Options.ConfigurationExtensions 包來讀取appsettings.json。具體的操做以下:數據庫

  使用NuGet添加  Microsoft.Extensions.Options.ConfigurationExtensions  包到咱們的項目當中,而後在appsettings.json中添加咱們本身的一些配置信息json

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "OpenAuthDBContext": "Data Source=localhost;Initial Catalog=dbname;User=sa;Password=123"
  },
  "AppSetting": {
    "SSOPassport": "http://localhost:52789",
    "Version": "1.0", //若是爲demo,則屏蔽Post請求
    "DbType": "SqlServer", //數據庫類型:SqlServer/MySql
    "MessageUrl": "http://localhot:23124", //短信平臺接口
    "MessageType": "CAD71325-0097-4052-9183-56F04EED0B31" //短信類型ID
  }
}

  而後咱們新建一個文件AppSettingapp

    /// <summary>
    /// 配置項
    /// </summary>
    public class AppSetting
    {

        public AppSetting()
        {
            SSOPassport = "http://localhost:52789";  
            Version = "";
            DbType = Define.DBTYPE_SQLSERVER;
        }
        /// <summary>
        /// SSO地址
        /// </summary>
        public string SSOPassport { get; set; }

        /// <summary>
        /// 版本信息
        /// 若是爲demo,則屏蔽Post請求
        /// </summary>
        public string Version { get; set; }

        /// <summary>
        /// 數據庫類型 SqlServer、MySql
        /// </summary>
        public string DbType { get; set; }

        /// <summary>
        /// 短信平臺接口Url
        /// </summary>
        public string MessageUrl { get; set; }

        /// <summary>
        /// 短信類型
        /// </summary>
        public string MessageType { get; set; }
    }

  接着在Startup.cs文件的ConfigureServices方法中添加spa

services.AddOptions();
//映射配置文件
services.Configure<AppSetting>(Configuration.GetSection("AppSetting"));

  最後就能夠在咱們的Controller中使用了,經過IOption<AppSetting>來讀取。code

private readonly IOptions<AppSetting> _setting;
 
public LoginController(IAuth authUtil,IOptions<AppSetting> setting)
{
       _authUtil = authUtil;
       _setting = setting;
}

public string GetCaptcha(string phone)
{
    string messageUrl = _setting.Value.MessageUrl;
    string messageType = _setting.Value.MessageType;

    if (_authUtil.GetCaptcha(phone, messageUrl, messageType))
    {
        return JsonHelper.Instance.Serialize(new { Code = 200, Message = "" });
    }
    return JsonHelper.Instance.Serialize(new { Code = 500, Message = "驗證碼獲取失敗,請稍後重試!" });
}

  須要注意的是,經過IOption的方式不能在Startup.cs中讀取appsettings.json文件,在Startup.cs中讀取appsettings.json文件須要使用Microsoft.Extensions.Configuration的IConfiguration。blog

var dbType = ((ConfigurationSection) Configuration.GetSection("AppSetting:DbType")).Value;

 或者使用索引

var v = Configuration["ASPNETCORE_ENVIRONMENT"];
var d = Configuration["AppSetting:MessageUrl"];

 針對格式爲下面的json,我麼能夠經過索引來獲取接口

{
    "Message": {
        "Message1": {
            "Name": ""
        },
        "Message2": {
            "Name": ""
        }
    }
}
_configuration["Message:0:Name"]
相關文章
相關標籤/搜索