Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IIS cannot get my environment variable

I would like to use environment variable in my web application.

I go to the advanced system settings and set my environment variable (for example MY_HOST_NAME) by system-level variables. Then I restart the server and check with the commandline "set" if the variable is there and I can see it. But if I try to get the variable within my application which is hosted by IIS, there is the variable null. The code which I'm using:

var host = Environment.GetEnvironmentVariable("MY_HOST_NAME");

Knows anyone where the problem could be or how can I find the bug?

like image 749
Jimy Weiss Avatar asked Apr 10 '17 11:04

Jimy Weiss


2 Answers

If by restarting server you mean Restart from IIS Manager, then this will not be enough. In case you don't want to restart the machine (who does?), you'll have to restart iis from an elevated prompt to pickup the newly changed variables

iisreset /restart

or in case one prefers some more typing

net stop w3svc && net start w3svc
like image 115
Dan Dohotaru Avatar answered Sep 18 '22 00:09

Dan Dohotaru


Is it a user environmental variable, or a system-level variable? The reason I ask is your IIS code is probably running as a different user. If your variable is a user variable, it wouldn't be present when the code runs as the other user.

Go to the control panel, system properties:

enter image description here

like image 22
rory.ap Avatar answered Sep 21 '22 00:09

rory.ap