Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

read web.config from another assembly using t4

Tags:

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

like image 927
almog.ori Avatar asked Jan 31 '10 12:01

almog.ori


2 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;
like image 57
Sagi Avatar answered Oct 21 '22 00:10

Sagi


You could try to do something like that:

var config = ConfigurationManager.OpenExeConfiguration("../somePathTo/web.config")
// use the config to get values like: config.AppSettings
like image 32
Robert Avatar answered Oct 21 '22 00:10

Robert