Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 233979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:08:51+00:00 2026-05-11T20:08:51+00:00

I have an entry in my Web.Config file that indicates which environment I am

  • 0

I have an entry in my Web.Config file that indicates which environment I am in for connection strings and junk:

<add key="AppEnv" value ="2" /> <!--(0 = Dev, 1 = test, 2 = prod)--> 

I am looking for a way to alert the developer, at the time of publishing, to make sure they have checked this key/value so that they don’t publish the ‘test’ to the ‘prod’ server and vice versa.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T20:08:52+00:00Added an answer on May 11, 2026 at 8:08 pm

    I have come up with my own (probably unconventional) solution to this issue. We develop many different web projects for many different clients and have migrated all of them to this method due to all of the issues we have had with multiple web.config files, or required edits before publishing.

    Basically, we let our app tell us which environment its running under based on the incoming URL. We initialize this on the first request and store it in memory for the life of the app. This way we can store each of our environment specific config values in the same config file and just qualify them with Development, Staging, Production, etc. And any settings that dont differ between environments dont need to be qualified.

    First a sample web.config:

    <appSettings>
        <add key="DevelopmentHost" value="dev.trackmyhours.com" />
        <add key="StagingHost" value="staging.trackmyhours.com" />
        <add key="ProductionHost" value="www.trackmyhours.com" />
      </appSettings>
    
      <connectionStrings>
        <clear />
        <add name="DevelopmentConnectionString" connectionString="your dev conn string" providerName="System.Data.SqlClient" />
        <add name="StagingConnectionString" connectionString="your staging conn string (mine is typically same as staging)" providerName="System.Data.SqlClient" />
        <add name="ProductionConnectionString" connectionString="your production conn string" providerName="System.Data.SqlClient" />
      </connectionStrings>
    

    Next we have an “App” class that gives us access to our “Site” class, but you could architect your classes however you see fit.

    Public Class App
    
        Private Shared _Site As New Site
        Public Shared ReadOnly Property Site() As Site
            Get
                Return _Site
            End Get
        End Property
    
    End Class
    

    Imports System.Configuration
    Imports System.Web
    
    Public Class Site
    
        Public Enum EnvironmentType
            Development
            Staging
            Production
        End Enum
    
        Friend Sub New()
    
            If HttpContext.Current IsNot Nothing Then
    
                Dim URL = HttpContext.Current.Request.Url.DnsSafeHost
    
                Select Case URL
                    Case ConfigurationManager.AppSettings("DevelopmentHost"), "localhost"
                        _Environment = EnvironmentType.Development
                    Case ConfigurationManager.AppSettings("StagingHost")
                        _Environment = EnvironmentType.Staging
                    Case ConfigurationManager.AppSettings("ProductionHost")
                        _Environment = EnvironmentType.Production
                End Select
    
            Else
                'probably getting called from a winforms/console app, or unit tests
                _Environment = EnvironmentType.Staging
    
            End If
    
            _ConnectionString = ConfigurationManager.ConnectionStrings(_Environment.ToString & "ConnectionString").ConnectionString
    
        End Sub
    
    
        Private _Environment As EnvironmentType
        Public Property Environment() As EnvironmentType
            Get
                Return _Environment
            End Get
            Set(ByVal value As EnvironmentType)
                _Environment = value
    
                _ConnectionString = ConfigurationManager.ConnectionStrings(_Environment.ToString & "ConnectionString").ConnectionString
    
            End Set
        End Property
    
        Private _ConnectionString As String
        Public ReadOnly Property ConnectionString() As String
            Get
                Return _ConnectionString
            End Get
        End Property
    
    End Class
    

    We put our classes in our Biz Object class library. We just decided that it is not necessary to determine the environment on every single request, since it really cant change during the app’s lifetime. Also, this allows us to reference App.Site.Environment from ANYWHERE in code in the library or code behind. This is also helpful if you need to sprinkle some conditional logic in your code – like not sending emails to real people when running in dev/staging.

    One last thing – for our Linq2SQL or EF Data/ObjectContexts, we do not store the connection string in the file, and instead overload the constructor so we can supply our correct environment connection string like this:

    Partial Class SampleDataContext
        Sub New()
            MyBase.New(App.Site.ConnectionString)
        End Sub
    End Class
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bowling web application that allows pretty detailed frame-by-frame information entry. One
I have a file that has one entry per line. Each line has the
When your web.config or app.config file has an appsettings entry, what is the best
Here is my situation: I have my mvc-config.xml file for my web service set
I have the following entry in the web.config: <customErrors mode=RemoteOnly defaultRedirect=Error.aspx /> Is there
I have an application web.xml with the following entry: <error-page> <error-code>404</error-code> <location>/system_files/error/p_notfound.jsp</location> </error-page> However,
I have a single entry output from a paradox table which is imported into
I have a set of 10 CSV files, which normally have a an entry
We have a number of very old data entry forms that were made in
Let's say that I have my ASP.NET web application in a directory called MyApp

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.