One of my colleague asked me how to read version number from Project.json file. He needed the version number for error logging. So I gave him the solution and thought of sharing with you. So in this quick post, find sample code to read Project.json file in ASP.NET Core 1.0.
// Read project.json file var projConfig = new ConfigurationBuilder().AddJsonFile("project.json").Build(); var appVersion = projConfig["version"]; //Output version number var mvcVersion = projConfig["dependencies:Microsoft.AspNet.Mvc"]; // Output MVC version
Below is the screenshot of projConfig
variable when viewed in quick watch while debugging. Just watch out here for key formation.
The above will also work for reading other JSON files. All you need to replace is name of json file and obviously the keys name.
Cheers!!!!
This is a great thing. I am implementing something similar and could use your example.
Could you please share your source code? I don’t seem to find it on a post