Aspnetcore: How to set environmental variable ASPNETCORE_ENVIRONMENT in IIS in the latest ASP.Net Core 1.0?

Created on 4 Aug 2016  路  11Comments  路  Source: dotnet/aspnetcore

Hi,

We use VS publish option to deploy to multiple environments ( Dev, Preview and Prod), so i added below commands in project.json file to set ASPNETCORE_ENVIRONMENT value dynamically in the web server as part of publish script. (This was setting environmental variable located in appsettings part of my web.config on my website in IIS). I was doing this to avoid a manual job of setting this ASPNETCORE_ENVIRONMENT variable in each deployment server

Things were working fine with RC2 libraries but it started failing with latest 1.0 libraries.
After analysis, i figured out that there was a environmentalvariables element in system.webServer/aspNetCore section of web.config in IIS (Accessed through ConfigurationEditor) and that was not set and hence our code was failing when we check Environment.IsDevelopemnt() function.

What is the command that i can use in project.json to set environmental variable (ASPNETCORE_Environment) in that path while publishing?

image

Project.Json: (Snippet)

 "publishOptions": {
    "include": [
      "wwwroot",
      "Views",
      "appsettings.json",
      "web.config"
    ]
  },

  "commands": {
    "Development": "Microsoft.AspNet.Server.Kestrel --ASPNETCORE_ENVIRONMENT Development",
    "Preview": "Microsoft.AspNet.Server.Kestrel --ASPNETCORE_ENVIRONMENT Preview",
    "Production": "Microsoft.AspNet.Server.Kestrel --ASPNETCORE_ENVIRONMENT Production"
  },

  "scripts": {
    "prepublish": [ "npm install", "bower install", "gulp clean", "gulp min" ],
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }

Thanks,

Most helpful comment

if some one is scrolling for answer thru here , try the steps at http://stackoverflow.com/a/36836533/4879683

All 11 comments

commands in project.json are obsolete. Environment is best managed in your runtime environment, not as part of your project. E.g. here's how to set it in VS's launchSettings.json file for running your app from VS: https://github.com/aspnet/Templates/blob/dev/src/BaseTemplates/StarterWeb/Properties/launchSettings.json#L8
Similarly for something like Preview you would set it as an environment variable on the server after deployment.

Thanks for your response Tratcher! Is it recommended to set the Special ASPNETCORE_Environment variable in the webconfig (System.Webserver part of config) or as general environmental variable in the web server?

The web.config flows with your app to different environments, so Environment should not be set there. Set it directly in your deployment environment instead.

What i meant was setting environmental variable collections in web.config using Configuration Editor in the webserver for this website
VS
Setting ASPNETCORE_Environment as System Environmental variable ?

using configuration editor changes your web.config, and those changes will be lost next time you deploy your app.

I realized that. Thanks Tratcher!

if some one is scrolling for answer thru here , try the steps at http://stackoverflow.com/a/36836533/4879683

This answer is completely unsatisfactory. Why are web.config transforms disabled for ASP.NET Core projects?

If I want to have have ASPNETCORE_ENVIRONMENT per site in IIS I have to do it in web.config right? How do I transform the config? Nope.

This dependency on a shared global env variable for every site that's deployed on a server is complete rubbish when it comes to IIS deployments. I understand you're trying to be platfor agnostic but the dev experience of deploying to IIS is utter confusion.

For example: how do you suggest I add this rewrite rule for production?

        <rewrite>
            <rules>
                <rule name="HTML5" stopProcessing="true">
                    <match url=".*" />
                    <conditions>
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="/" />
                </rule>
            </rules>
        </rewrite>

@oliverjanik try dotnet-transform-xdt; an example for adding rewrite rules is described here: https://github.com/nil4/xdt-samples/#production-iis-config

Hello, does someone provide insight on how to set environment variable while moving it to test or production for console applications such as windows services that are non-web applications.?

That depends entirely on your launching environment. For a windows service you could provide the same information as command line parameters.

Was this page helpful?
0 / 5 - 0 ratings