Skip to content

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

@valcs007

Description

@valcs007

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,

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions