欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

.net core 读取、修改配置文件appsettings.json

程序员文章站 2022-07-10 23:43:12
.net core 设置读取JSON配置文件 appsettings.json Startup.cs 中 类库中 使用 这种方法可以保证可以修改正在运行的dotnet core 程序,不需要重启程序 使用Newtonsoft.Json 查询/修改,以及修改 appsettings.json 文件 R ......

.net core 设置读取json配置文件 appsettings.json

startup.cs 中

    public class startup
    {
        public startup(ihostingenvironment env)
        {
            configuration = new configurationbuilder()
                .setbasepath(env.contentrootpath)
                .addjsonfile("appsettings.json", optional: true, reloadonchange: true)
                .addjsonfile($"appsettings.{env.environmentname}.json", optional: true, reloadonchange: true)  //增加环境配置文件,新建项目默认有
                .addenvironmentvariables()
                .build();
        }
        public iconfiguration configuration { get; }
....

类库中

            var builder = new configurationbuilder()
                .setbasepath(directory.getcurrentdirectory())
                .addjsonfile("appsettings.json")
                .addjsonfile("appsettings.test.json", true, reloadonchange: true);

            var config = builder.build();

            //读取配置
           var a = config["jwtsettings:secret"];

使用.addjsonfile("appsettings.json", optional: true, reloadonchange: true)这种方法可以保证可以修改正在运行的dotnet core 程序,不需要重启程序

使用newtonsoft.json 查询/修改,以及修改 appsettings.json 文件

readobject

        public iactionresult readobject()
        {
            string json = @"{
   'cpu': 'intel',
   'psu': '500w',
   'drives': [
     'dvd read/writer'
     /*(broken)*/,
     '500 gigabyte hard drive',
     '200 gigabyte hard drive'
   ]
}";

            jsontextreader reader = new jsontextreader(new stringreader(json));
            var string1 = "";
            while (reader.read())
            {
                if (reader.value != null)
                {
                    string1 += "reader.value != null: token: " + reader.tokentype + ", value: " + reader.value + " <br/>";
                }
                else
                {
                    string1 += "reader.value == null: token: " + reader.tokentype + " <br/>";
                }
            }

            viewdata["string"] = string1;
            return view();
        }

.net core 读取、修改配置文件appsettings.json

readjson 读取json 文件

newtonsoft.json

使用newtonsoft.json效果如截图

    public class homecontroller : controller
    {
        private readonly ihostingenvironment _hostingenvironment;
        public homecontroller(ihostingenvironment hostingenvironment)
        {
            _hostingenvironment = hostingenvironment;
        }

        public iactionresult readjson()
        {
            string contentpath = _hostingenvironment.contentrootpath + @"\"; ;   //项目根目录
            var filepath = contentpath + "appsettings.json";
            using (streamreader file = new streamreader(filepath))
            using (jsontextreader reader = new jsontextreader(file))
            {
                jobject o2 = (jobject)jtoken.readfrom(reader);

                string licencekey = (string)o2["appsettings"]["key"];

                viewdata["string"] = licencekey;
                return view();
            }
        }

.net core 读取、修改配置文件appsettings.json

microsoft.extensions.configuration

使用microsoft.extensions.configuration

        public iactionresult index()
        {
            var builder = new configurationbuilder()
                .setbasepath(directory.getcurrentdirectory())
                .addjsonfile("appsettings.json")
                .addjsonfile("appsettings.test.json", true, reloadonchange: true);

            var config = builder.build();

            //读取配置
            viewdata["secret"] = config["appsettings:key"];
            return view();
        }

.net core 读取、修改配置文件appsettings.json

修改 appsettings.json后的效果

.net core 读取、修改配置文件appsettings.json

  • newtonsoft.json
    .net core 读取、修改配置文件appsettings.json

  • microsoft.extensions.configuration
    .net core 读取、修改配置文件appsettings.json

修改 appsettings.json

streamwriter直接覆盖

    public class homecontroller : controller
    {
        private readonly ihostingenvironment _hostingenvironment;
        public homecontroller(ihostingenvironment hostingenvironment)
        {
            _hostingenvironment = hostingenvironment;
        }
        public iactionresult writejson()
        {
            string contentpath = _hostingenvironment.contentrootpath + @"\"; ;   //项目根目录
            var filepath = contentpath + "appsettings.json";
            jobject jsonobject;
            using (streamreader file = new streamreader(filepath))
            using (jsontextreader reader = new jsontextreader(file))
            {
                jsonobject = (jobject)jtoken.readfrom(reader);
                jsonobject["appsettings"]["key"] = "asdasdasdasd";
            }

            using (var writer = new streamwriter(filepath))
            using (jsontextwriter jsonwriter = new jsontextwriter(writer))
            {
                jsonobject.writeto(jsonwriter);
            }

            return view();
        }

效果如图
.net core 读取、修改配置文件appsettings.json
.net core 读取、修改配置文件appsettings.json

缺点 格式化的json和注释都没了