Read web.config from another build using t4

Does anyone have a good example or helper class that would allow me to read the connection string in a web application from the T4 template located in the ANOTHER assembly referenced by the web application. I am generating some code from the database that it is referencing, and I would offer some help on how to get the connection string for this use. ive read the example of George Js here , however it only works when the template is in the web application, please help !!!

+5
source share
3 answers
var path = Host.ResolvePath(@"../Web.config");  
var map = new ExeConfigurationFileMap { ExeConfigFilename = path };           
var config =  ConfigurationManager.OpenMappedExeConfiguration(
                                            map,ConfigurationUserLevel.None);  
var appSettings = config.AppSettings;  
var connectionStrings = config.ConnectionStrings.ConnectionStrings;
+3

- :

var config = ConfigurationManager.OpenExeConfiguration("../somePathTo/web.config")
// use the config to get values like: config.AppSettings
+1

after a little search around ive found my answer in t4, you can use

path = Host.ResolvePath(relativeFileName)
0
source

All Articles