How to get environment variables information in asp.net

Murari Ram 21 Reputation points
2022-04-04T14:35:48.99+00:00

Hi All,

We are using window server 2019 and VS2017 .net frame work 4.8. We have create environment variable(s). How to access these variables and variables information from asp.net . not from core.

<add name="TestProj" autoStart="true" managedRuntimeVersion="v4.0">
<environmentVariables>
<add name="TestID" value="xxxx-xxx-xxx" />
<add name="TestClientID" value="yyyy-yyy-yyy" />
</environmentVariables>
</add>

Developer technologies ASP.NET Other
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Albert Kallal 5,586 Reputation points
    2022-04-04T16:05:44.51+00:00

    well, in asp.net and using IIS, there is not really a section called environment variables.

    However, it common to have a bunch of settings and variables for use throughout the applcation.

    And as a general rule, web.config is used for this purpose.

    I mean, because web.config can often be quite large, messy and have a lot of sections?

    Well, then for the most part, I often use and let project settings for this purpose.

    so, go project->"my project name" properties, and we have this:

    189861-image.png

    So in above, we can have conneciton settings, even simple things like say some colors for backgrounds, and all kind of things - even things like location information etc.

    For example, in above, I have highlighted the value CompanyName. Now, in code I am free to use that setting like this:

    Say I have a div with runat=server, then in code I can do this:

            Dim strWelcomMessage = "Welcome to " & My.Settings.CompanyName  
      
            Me.WelcomeMsg.InnerHtml = "<h2>" & strWelcomMessage & "</h2>"  
      
    

    And now we see this on the web page:

    189852-image.png

    So, just use the application settings. put your values, connection strings etc. into the project settings.

    So, I not aware that any "environment" tags can be placed in your web config (it will actaully result in a compile error from IIS complaining about that tag not legal in your web confiig.

    However, you are certainly free to setup string and even connection strings as per above.
    In fact, I prefer using the project settings, since the alternative is deep diving into the web.config file, and that always a big hairy cat ball of markup - which I don't like to mess with.

    For the most part, we consider these values read-only anyway, so it not at all clear why your looking to use a "environment" tag, since we have the long time and long used web.config for such settings anyway.

    At the end of the day, the goal here is:

    How and where can I put a bunch of the application and web site settings I need?

    And the answer in general is to simple use the Project->My Project settings.

    There might be some other issue here? but in standard asp.net, and say .net 4.8? Just use the project settings. Those settings most certainly wind up in web.config, but I not sure that matters.

    So, now, we don't have something called "environment variables" in .net, but we have a place for such settings in our applications.

    0 comments No comments

  2. Yijing Sun-MSFT 7,096 Reputation points
    2022-04-05T03:30:29.42+00:00

    Hi @Murari Ram ,
    As far as I know, there are no "environment variables" in .net. So, I'm thinking about your problem from another angle. Your source codes just like some nodes and the " environment variables" maybe means the node<environmentVariables>. You want to get the child node “TestID”,"TestClientID" and their values inside the node <environmentVariables>. Right?

    Best regards,
    Yijing Sun


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our  documentation  to enable e-mail notifications if you want to receive the related email notification for this thread.


  3. AgaveJoe 30,126 Reputation points
    2022-04-07T10:30:42.06+00:00

    The web.config sample you've shared is for ASP.NET Core. You tagged this post as web forms and asp general which does not use this construct.

    If this is an asp.net application then follow AlbertKallal-4360's advice.

    if this is a .NET Core question then standard constructor injection is one option. Other options are explained official configuration docs.

    Configuration in ASP.NET Core

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.