2016-12-01 8 views
1

Web.configファイルにはWebアプリケーションを実行するときにDebugモードかReleaseモードかによっていくつかのエントリがあります。ローカル、私は別のappSettingsを取るしたい。Web.Debug.configとローカルホストのWebアプリケーションを実行しているWeb.Release.config

たとえば、Web.Debug.config appSettingsに次のエントリがあるとします。

<add key="MyServiceUrl" value="http://my-test-site:8080/" /> 

そしてまた、私は私のWeb.Release.configでこれを持っている:

<add key="MyServiceUrl" value="http://my-prod-site:80/" /> 

私は私のWeb.Config、Web.Debug.ConfigとWeb.Release.Configそうに応じて設定する必要がありますどのようにモードは私のアプリケーションをローカルで実行します(デバッグ - すべてのCPUリリース - すべてのCPU)、それは正しいものですか?

現時点では、キーと値の唯一のペアは、Visual Studioで[デバッグ]または[リリース]を選択するかどうかにかかわらず、メインWeb.Configのものです。

どうすればこのような動作を設定できますか?

EDIT

これは、私はこれは私がこれは私が持っているかであるWeb.Debug.config

<appSettings> 
    <add key="MyServiceUrl" value="http://my-test-site:8080/" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/> 
</appSettings> 

を定義している方法ですWeb.configファイル

<appSettings> 
    <add key="webpages:Enabled" value="false" /> 
    <add key="ClientValidationEnabled" value="true" /> 
    <add key="UnobtrusiveJavaScriptEnabled" value="true" /> 
</appSettings> 

を定義している方法です定義済みWeb.Release.config

<appSettings> 
    <add key="MyServiceUrl" value="http://my-prod-site:8080/" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" /> 
</appSettings> 
012私はこのようにそれを呼び出す

public static string GetAppSetting(string settingKey) 
     { 
      if (string.IsNullOrEmpty(settingKey)) 
       throw new System.ArgumentException("Cannot fetch a setting for a null/empty setting key."); 
      return ConfigurationManager.AppSettings[settingKey]; 
     } 

最後に、私のコードでは、私は次のような方法持っ 文字列の設定を= GetAppSetting( "MyServiceUrl");それはweb.Release.configでメインのWeb.config

+0

_

+0

または、次のようにしてください:_xdt:Transform = "Insert" _ –

答えて

1

これを試すには定義されていない場合

しかし、それがnullの場合、それが動作するはずです:

<appSettings> 
<add key="MyServiceUrl" value="http://my-prod-site:8080/" xdt:Transform="Insert" /> 
</appSettings> 

これを読む:Web.config Transformation Syntax for Web Application Project Deployment

+0

残念ながら、まったく同じことが起こります。エントリがweb.configに存在しない場合は、GetAppSettingを呼び出すと、エントリが生成されません。そして、もしそれが存在すれば、メインweb.configにあり、デバッグやリリースのものではありません。 – user3587624

+0

** static **を削除すればどうなりますか? from:_public静的な文字列GetAppSetting(string settingKey)_ –

+0

同じことです。メインweb.configから取得します – user3587624

関連する問題