2011-05-25 5 views
0

開発、QA、およびプロダクションの3つの環境で動作するアプリケーションがあります。アプリケーションはSQLサーバーといくつかのWebサービスにアクセスします。 web.configファイルには、SQLサーバーの接続文字列とWebサービスのIPアドレスがあります。私たちは、3つの環境すべてで動作するweb.configファイルを1つ作ることができるようにしたいと思いますが、どうにかして各環境のさまざまなデータを取得します。誰かがこれを行う方法を知っていますか?実行時にweb.configに値を代入する

答えて

1

Web.config Transformationをご覧ください。

+0

この優秀なリファレンスをありがとう。私はこの能力が存在することを知らなかった。 – dna86

0

私たちはあなたとまったく同じ設定オプションを使用して、我々はこのような私たちのweb.configファイルで3つの接続文字列を作成しました:

<connectionStrings> 
    <add name="Dev" connectionString="Server=localhost;Database=WebDev;User=devo;Pwd=xxxxx;" providerName="System.Data.SqlClient"/> 
    <add name="Stage" connectionString="Server=localhost;Database=WebStage;User=stago;Pwd=xxxxx;" providerName="System.Data.SqlClient"/> 
    <add name="Live" connectionString="Server=localhost;Database=WebLive;User=livo;Pwd=xxxxx;" providerName="System.Data.SqlClient"/> 
</connectionStrings> 

その後、我々が決定するURLに自分自身を基づか静的メソッドを持っていますどの連絡先を使用するか:

public static string ConnStr 
{ 
    get 
    { 
     if (Config.WebRoot.StartsWith("http://www.")) { return ConfigurationManager.ConnectionStrings["Live"].ToString(); } 
     else if (Config.WebRoot.StartsWith("http://stage.")) { return ConfigurationManager.ConnectionStrings["Stage"].ToString(); } 
     else if (Config.WebRoot.StartsWith("http://localhost")) { return ConfigurationManager.ConnectionStrings["Dev"].ToString(); } 
     else { return null; } 
    } 
} 

あなたはあなたのために方法を調整する必要があります。

関連する問題