.net core 2.0配置文件的使用

内容如题,不罗嗦了。

1.打开appsettings.json,添加我需要的AppSettings

{
  "Logging": {
    "IncludeScopes": false,
    "Debug": {
      "LogLevel": {
        "Default": "Warning"
      }
    },
    "Console": {
      "LogLevel": {
        "Default": "Warning"
      }
    }
  },
  "AppSettings": {
    "AddressMatchUrl": "http://192.168.200.191:8813/AddressMatch?request=1&q=",
    "AddressMatchDownloadFile": "http://localhost:9957/download/addressmatch/",
    "MaxRepeatRequestNum": "30"
  }
}

2.新建一个AppSettings.cs的类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace DotnetCoreWebAPI
{
    public class AppSettings
    {
        /// 
        /// 地址匹配引擎服务地址
        /// 
        public string AddressMatchUrl { get; set; }
        /// 
        /// 匹配结果下载地址
        /// 
        public string AddressMatchDownloadFile { get; set; }
        /// 
        /// 请求失败最大尝试次数
        /// 
        public string MaxRepeatRequestNum { get; set; }

    }
}

3.打开startup.cs修改

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
            services.Configure(Configuration.GetSection("AppSettings"));
        }

4.引用的代码里注入

        public AppSettings appseting;
        public ProgressController(IOptions appSettings)
        {
            appseting = appSettings.Value;
        }

5.具体使用

int maxreapet = Convert.ToInt32(appseting.MaxRepeatRequestNum);


完事~

你可能感兴趣的:(开发中遇到的问题记录,.Net,Core)