Link to home
Start Free TrialLog in
Avatar of volking
volking

asked on

Is a GENERIC web.config possible?

I build ASP.NET websites on my local box, then PUBLISH them to webservers.

I have about a dozen WEB.CONFIG settings, which need to change, every time I publish to a webserver. So, I've been EXCLUDING the WEB.CONFIG from my projects so they don't overlay the differences on each webserver ....

Arrrrrrrgh! Meaning, any time I make a web.config change, I gotta MANUALLY update all the other webserver's web.config files ....

Has anyone successfully built a GENERIC web.config?

Meaning, all web.config settings, for ALL webservers, are in one web.config, and the SERVER decides, based on it's NAME, which section of the Web.Config to use?

Avatar of Bob Learned
Bob Learned
Flag of United States of America image

What kind of settings are you talking about here?  I am going to assume ASP.NET 2.0.

Bob
Avatar of donkitchen
donkitchen

I use an alternate solution where I have multi-layered config files.  So, for example, I have my main web.config, which keeps application settings in an external file, externalsettings.config.

I then have a few different externalSettings files with different settings.  Like externalSettings.test, externalSettings.prod.  You can then via various methods (post build steps, batch files, etc) rename the target file to the proper externalSettings.config based on which environment (or server) you are deploying to.
<appSettings file="externalSettings.config"/>

Open in new window

You can also set the settings in the Machine.Config, so that all are applied to all the sites in the server. No need to change in the web.config.
Avatar of volking

ASKER

@TheLearnedOne - like these kind of settings  

<connectionStrings>
    <add name="LocalSqlServer" connectionString="Server=XXX;Initial Catalog=XXX;User=XXX;password=XXX;" providerName="System.Data.SqlClient"/>
</connectionStrings>
Avatar of volking

ASKER

@donkitchen - yep, that's exactly what I'm trying to AVOID. I want the CONFIG file (plus maybe the current server name) to auto-magically use a different setting
Avatar of volking

ASKER

ok ... if I were to imagine HOW it might look ..... what I'm loooking would look SOMETHING LIKE THIS ....
<configuration>
   <connectionStrings>
      <HOSTNAME="XXX">
         <add name="LocalSqlServer" connectionString="Server=AAA;Initial Catalog=MyDB;"/>
      </HOSTNAME="XXX">
      <HOSTNAME="YYY">
         <add name="LocalSqlServer" connectionString="Server=BBB;Initial Catalog=MyDB;"/>
      </HOSTNAME="YYY">
      <HOSTNAME="ZZZ">
         <add name="LocalSqlServer" connectionString="Server=DDD;Initial Catalog=MyDB;"/>
      </HOSTNAME="ZZZ">
   </connectionStrings>
</configuration>

Open in new window

You can have multiple connection strings in a single connection string, and pull the correct one based on something like host name using ConfigurationManager.ConnectionStrings.

Bob
Avatar of volking

ASKER

@TheLearnedOne

Sure, using code, one can do anything. But what about inter-dependent settings. The below membership, profile and roleManager settings all rely on a NAMED CONNECTIONS string. One cannot rely on code to use the correct ConnestString ...

Thus this question about truely generic .CONFIG files.
  <connectionStrings>
    <clear/>
    <add name="SqlServices" connectionString="Server=XXX;Initial Catalog=XXX;User=XXX;password=XXX;"
        ProviderName="System.Data.SqlClient"/>
  </connectionStrings>
  <system.web>
    <membership 
      defaultProvider="SqlProvider" 
        userIsOnlineTimeWindow="20" 
        hashAlgorithmType="SHA1">
      <providers>
        <clear/>
        <add name="SqlProvider"
          type="System.Web.Security.SqlMembershipProvider"
          connectionStringName="SqlServices"
          enablePasswordRetrieval="false"
          enablePasswordReset="true"
          requiresQuestionAndAnswer="true"
          passwordFormat="Hashed"
          applicationName="/"
          requiresUniqueEmail="true"
          maxInvalidPasswordAttempts="5"
          minRequiredPasswordLength="4"
          minRequiredNonalphanumericCharacters="0"
          passwordAttemptWindow="10"
          passwordStrengthRegularExpression="" />
      </providers>
    </membership>
    <profile defaultProvider="SqlProvider">
      <providers>
        <clear />
        <add name="SqlProvider"
          type="System.Web.Profile.SqlProfileProvider"
          connectionStringName="SqlServices"
          applicationName="SampleApplication"
          description="SqlProfileProvider for SampleApplication" />
      </providers>
    </profile>
    <roleManager cacheRolesInCookie="true" cookieName=".ASPROLES"
      cookieRequireSSL="true" defaultProvider="SqlProvider">
      <providers>
        <clear />
        <add connectionStringName="SqlServices" applicationName="asbusa"
          name="SqlProvider" type="System.Web.Security.SqlRoleProvider" />
      </providers>
    </roleManager>    
  </system.web>

Open in new window

Avatar of volking

ASKER

@TheLearnedOne - in the above sample, notice lines 15 & 34 reference a named connectstring called "SqlProvider" which is defined in line 3. The "trick" is using a different Line 3 based on hostname (or some such host-relative value).

That's what I'm looking for. A way to dynamicly change line 3 based on SOMETHING? And thus, changing line 3, causes functional cascades through the entire .CONFIG file.

Fred
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of volking

ASKER

@TheLearnedOne

Smile ... thank you ... sometimes the right answer is "No" .... I didn't think it was possible, but thought asking others, "wouldn't hurt" ...