ASP.NET Core MVCプロジェクトにAngular 2アプリケーションがあります。 Angular 2アプリケーションとStartup.cs
の両方に、特定の環境、すなわち、 http://devserver
の代わりにhttp://localhost
をWebサービスのURLとして使用してください(公開時に使用する必要があります)。私はこれをプログラムで行う必要があるので、OSでASPNETCORE_ENVIRONMENT
を設定しないと、これをしないでください。これを達成する方法はありますか?Angular 2とASP.NET Core MVCの環境変数を共有する方法
答えて
私はこれを行うことができました。それが最適な解決策であるかどうかは分かりませんが、それは私のために働きます。
私は、環境変数ベースではなく、解決策としてconfigの設定ベースファイル(つまり、デバッグ設定の場合は.Debug.fileを使用)を選択しました。
作成appsettings.json
これは、F5キーを押してプロジェクトをデバッグモードで実行すると読み込まれます。
{
"AppSettings": {
"MyApiUrl": "http://localhost:23224/v1/"
}
}
appsettings.Debug.json
を作成します。また、 "Copy to Output directory: Copy if newer
" appsettings.json
を設定します。あなたは(あなたが発行するためのDebug
構成を使用していると仮定して)プロジェクトを発行するときに使用されます:あなたはビルド時に/wwwroot
にappsettings.json
ファイルをコピーするAfterBuild
イベントを追加する
{
"AppSettings": {
"MyApiUrl": "http://api.server.com/v1/"
}
}
編集し.csproj
(あなたの角度2のアプリケーションがあると仮定するとwwwrootフォルダに)ので、角度、それを読むことができます:
<Target Name="AfterBuildOperations" AfterTargets="AfterBuild">
<Copy SourceFiles="$(ProjectDir)appsettings.json" DestinationFiles="$(ProjectDir)wwwroot\appsettings.json" OverwriteReadOnlyFiles="true" />
</Target>
編集して.csproj
appsettings.json
からappsettings.Debug.json
の名前を変更し、あなたがそれを公開するとき、それがwwwroot
フォルダにコピーします。
<Target Name="AfterPublishOperations" AfterTargets="AfterPublish">
<Delete Files="$(ProjectDir)bin\$(ConfigurationName)\PublishOutput\appsettings.json" />
<Copy SourceFiles="$(ProjectDir)bin\$(ConfigurationName)\PublishOutput\appsettings.$(ConfigurationName).json" DestinationFiles="$(ProjectDir)bin\$(ConfigurationName)\PublishOutput\appsettings.json" />
<Delete Files="$(ProjectDir)bin\$(ConfigurationName)\PublishOutput\appsettings.$(ConfigurationName).json" />
<Copy SourceFiles="$(ProjectDir)bin\$(ConfigurationName)\PublishOutput\appsettings.json" DestinationFiles="$(ProjectDir)bin\$(ConfigurationName)\PublishOutput\wwwroot\appsettings.json" OverwriteReadOnlyFiles="true" />
</Target>
設定は強く型付けされ得るためにあなたのプロジェクトにAppSettings.cs
モデルを追加します:
public class AppSettings
{
public string MyApiUrl { get; set; }
}
何らかの理由VS.NETのために発行する出力フォルダ内appsettings.jsonとappsettings.Debug.jsonの両方が含まれて公開
読むappsettings.json
Startup.cs
内のコンテンツとDIコンテナにシングルトンとしてそれを追加します。
public void ConfigureServices(IServiceCollection services)
{
var appSettings = ReadConfiguration();
services.AddSingleton(appSettings);
}
public AppSettings ReadConfiguration()
{
var section = Configuration.GetSection("AppSettings");
var settings = new AppSettings();
new ConfigureFromConfigurationOptions<AppSettings>(section).Configure(settings);
return settings;
}
あなたはAを注入することができますお使いのコントローラにppSettings:
private AppSettings appSettings;
public MyController(AppSettings appSettings)
{
this.appSettings = appSettings;
}
は今、あなたはどこでもあなたが角度2でほしいそれを読むことができ、あなたの角度2
export interface AppSettings {
MyApiUrl?: string;
}
にAppSettings.ts
を追加します。
private ReadAppSettings() {
this.http.get('/appsettings.json')
.map(response => response.json())
.subscribe(result => {
let appSettings: AppSettings = <AppSettings>result.AppSettings;
},
error => { console.error(error); });
}
- 1. 共有(Webファーム)ASP.NET Web 2層環境
- 2. ドッカーで環境変数を共有するには
- 3. Angular CLIのコンパイルステップのアクセス環境変数
- 4. Pythonのvirtualenv環境を共有する
- 5. ASP.NET CoreとAngular 2プロジェクトのtsc
- 6. Angular 2からASP.NET Core WebAPIにデータを投稿する方法
- 7. WindowsとLinux環境の両方でGemfile.lockを共有する
- 8. ASP.NET Core Angular 2 Webpackホットモジュールの交換
- 9. 2つのazureml環境間のデータセットを共有する
- 10. 共有ホスティング環境でのCakePHPと.htaccess
- 11. Asp.Netコアホスティング環境変数
- 12. IdentityServer4:ASP.NET Core MVCサーバーアプリケーションとJavaScriptクライアントの間の認証を共有する
- 13. 2 appとAngularJSの間で変数を共有する方法
- 14. Angular 2環境設定をAngular-Cliで設定する方法は?
- 15. ANGULAR 2 - コンポーネント共有データサービス
- 16. 環境変数を光源とする環境変数
- 17. Angular 2 with .NET Core
- 18. 私のクラスの共有環境設定の使用方法
- 19. 複数の環境でSQSを共有する
- 20. 共有ホスティング環境でasp.netサーバーからjson応答を圧縮
- 21. ASP.net mvc共有クラスライブラリ
- 22. WebPackビルド環境でCryptoJSをAngular 2とTypeScriptで使用する方法は?
- 23. Asp.net core、Angular 2. CookieAuthの問題
- 24. ASP.NET Core:開発環境変数のページロード時間が非常に遅い
- 25. CORSをASP.Net Core Web APIサーバーと連携させる方法Angular 2クライアント
- 26. Amazon Lambda - エイリアス固有の環境変数
- 27. androidの共有環境設定でいくつの共有環境設定を知るか
- 28. 仮想環境やPythonインストールの間でライブラリを共有する方法
- 29. gdb固有の環境変数
- 30. ASP .Net Core(non-mvc)でAngular 2ルーティング
あなたが達成したいかどうこの?異なる環境の懸念を分離したいのですか、アプリケーションの実行中にこれらの値を変更したいと思っていますか? – eminlala