Building a JSON Configuration Section

后端 未结 5 1421
轮回少年
轮回少年 2021-02-18 20:37

Is there a way to have configuration sections written in JSON instead of XML?

Let\'s suppose I have the following ConfigurationSection:

publ         


        
5条回答
  •  失恋的感觉
    2021-02-18 21:21

    If I understand correctly you essentially want to define web.config sections as JSON rather than XML.

    There is currently nothing out of the box that does this that I am aware of. However one possible solution is to use Gulp to dynamically write your web.config file as part of your build step. I don't have a concrete example as I'm not aware of anyone doing this, but this might give you a pointer.

    First have a look at this article that discusses how you can use the xmlpoke module to write XML:

    http://www.mikeobrien.net/blog/using-gulp-to-build-and-deploy-dotnet-apps-on-windows/

    Reading JSON in Gulp would be a piece of cake, so you just need to map the JSON into XML. You can then add the Gulp task to your build step by editing your .proj xml file (gulp should be installed globally on the machine the build is being performed on).

    1. Open your proj file in a text editor and locate this section:

      
          
          
          
      
      
    2. Replace the comment with a gulp command

      gulp taskname
      

    An alternative is to use a library such as Newtonsoft to read a JSON file from the disk. Then create your own attributes and system for mapping the values to the properties in your class. This is not straight forward but could certainly be done with some effort.

提交回复
热议问题