angularasp.net-coresingle-page-application

How to set SpaProxyServerUrl based on environment in .net core 6


In the SPA project Angular 13 with Asp.net Core 6. I want to set the SpaProxyServerUrl based on the environments like Dev, Stage and Prod. In the csproj file i have the code as below. Can we able to set the property "SpaProxyServerUrl" in program.cs file or is there any way to set the value dynamically based on the environment?

<PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
    <IsPackable>false</IsPackable>
    <SpaRoot>ClientApp\</SpaRoot>
    <SpaProxyServerUrl>http://localhost:4200</SpaProxyServerUrl>
    <SpaProxyLaunchCommand>npm start</SpaProxyLaunchCommand>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>

Solution

  • Found the solution myself. Using "Choose" tag we can set SpaProxyServerUrl value as below

    <Choose>
            <When Condition="'$(EnvironmentName)' == 'dev'">
                <PropertyGroup>
                    <SpaProxyServerUrl>https://localhost:4200</SpaProxyServerUrl>
                </PropertyGroup>
            </When>
            <When Condition="'$(EnvironmentName)' == 'poc'">
                <PropertyGroup>
                    <SpaProxyServerUrl>https://localhost:4201</SpaProxyServerUrl>
                </PropertyGroup>
            </When>
    </Choose>