Setting Environment When Publishing An MVC 6 Web App

孤街醉人 提交于 2019-12-11 05:48:32

问题


I'm testing out development in Dot Net Core, and I'm up to deploying my application. For now I've done a simple file publish to my local PC, and then copied the files onto the remote server.

When I view the website, I can see that it is running in either the Staging or Production environment due to me being served minified files (Which are specified in the view).

However I'm struggling to find any documentation to say where or how this environment can be specified at publish time. Even if I have to edit this setting directly for now that is fine, but I can't seem to find it.

Files I've checked after publish (That also includes the source files as I'm just publishing via VS2015 at the moment over getting to know the DNU commandline).

  • wwwroot/web.config
  • approot/web.cmd
  • approot/global.json
  • approot/src/myproject/project.json
  • approot/src/myproject/hosting.json
  • approot/src/myproject/project.json
  • approot/src/myproject/Properties/launchSettings.json

回答1:


Well that was a bit of fun. But here's the answer for a Windows machine.

The variable is system wide environment variable, I'm not sure how that's going to fly if you had two different configurations on the same machine (e.g. We have Test1, Test2, Test3 etc for different teams all on the same machine), although I suppose if you are getting to that size, you might be using something like Chef/Puppet to store configurations.

How you can set the variable system wide.

  1. Right click on "My PC", "This PC", "My Computer" or the variant that you have with whichever windows you are on. Select Properties.
  2. Hit Advanced System Settings (On Win10 this is on the left hand side).
  3. Click Environment Variables down the bottom of this next window.
  4. Click "New..." on "System Variables". Then enter in "ASPNET_ENV" with your specified environment like so....

  1. Now reset IIS and you should be good to go.

I have seen other people suggest that "Hosting:Environment" is the new variable name but this did not work for me, maybe it will work for you! I can see that within launchSettings.json it uses the hosting variable, but it didn't work for me when I put it machine wide. I suppose that's the the fun of fiddling with Release Candidates! :)

Another thing to note. When launching from Visual Studio, the environment it runs in is kept in /Properties/launchSettings.json

"IIS Express": {
  "commandName": "IISExpress",
  "launchBrowser": true,
  "environmentVariables": {
    "Hosting:Environment": "Development"
  }
}

You can edit the Hosting:Environment variable here to auto launch in a different environment.

However when you run from regular IIS, the default is always "Production" (Can't see that ever going wrong.....).



来源:https://stackoverflow.com/questions/37668760/setting-environment-when-publishing-an-mvc-6-web-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!