您的位置:首页 > 移动开发

C# Core1.1 版本 AppSettings 使用方法

2017-05-05 11:34 239 查看
1:在AppSettings.json 里书写格式为:"AppSettings":{"key":"value","key":"value"};

2::Start up.cs里新增配置   

 public void ConfigureServices(IServiceCollection services)

       {

            services.AddOptions();

            services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));

             services.AddMvc();

        }
3:新建一个AppSettings.cs  封装 所需要的Key;

4:管理NUGET包安装以下

"Microsoft.Extensions.Options.ConfigurationExtensions": "1.1.1",

"Microsoft.Extensions.Configuration.Json": "1.1.1“

5:在所需要的地方加代码

public clss XXXController:Controller

{ private AppSettings AppSettings { get; set; }

        public HomeController(IOptions<AppSettings> settings)

        {

            AppSettings = settings.Value;

        }

}

6:现在就可以在其他地方调用了

例如:var x = AppSettings.Key
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐