ASP.NET 5 (vNext) - Configuration

前端 未结 3 1986
耶瑟儿~
耶瑟儿~ 2021-02-08 18:58

I am trying to learn ASP.NET 5. I am using it on Mac OS X. At this time, I have a config.json file that looks like the following:

config.json

         


        
3条回答
  •  终归单人心
    2021-02-08 19:24

    You can simply access the value of config property Environment by using Configuration class like this anywhere in your application.

    var environment = Configuration["AppSettings:Environment"];
    

    But if you want it to access through a property of class you can do this

    public class AppSettings 
    { 
     public string Environment
     { 
     get 
     { 
      return new Configuration().Get("AppSettings:Environment").ToString(); 
     }
     set; 
     } 
    }
    

    Note I haven't set the value of Environment in setter because this value will be in config.json so there is no need to set this value in setter.

    For more details have a look at this article.

提交回复
热议问题