Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

.NET 4 running two sites 1 = TEST, 2 = Production, can I tell which is which in global.asax

Status
Not open for further replies.

DougP

MIS
Dec 13, 1999
5,985
US
I don't want to get the two sites mixed up. I have 2 SQL databases as well. so I need two connection strings.
Is there a way to have an 'IF' statement in the global.asax file to tell which folder on the server the global.asax file is sitting in when it runs, then set Session(WhereAmI") = ???? .
is there a way to do this somehow?
IF folder = "TEST" then
Session(WhereAmI") = "TEST"
Else
Session(WhereAmI") = "Production"
End if




DougP
 
It probably can be done, but a lot of work. Just use the web.config and have both connection strings in there. Then just comment out the one you don't want to connect to.
 
In Visual Studio 2010, you can use the web.config transforms to corral your connection strings. If you used the Web Application template, you should see that the Web.config is grouped with Web.Debug.config and Web.Release.config. When you publish, it uses your current configuration to determine which transform to use. For example...

In the Web.config, you can have all of the test connections set up
Code:
<?xml version="1.0"?>
<configuration>
    <connectionStrings>
        <add name="SQLServerConnectionString"
           connectionString="Server=TestServer;Initial Catalog=TestDB;User ID=USERID;Password=PASSWORD"
           providerName="System.Data.SqlClient"/>
    </connectionStrings>
</configuration>

In the Web.Debug.config, you don't need to put anything in because it will use the base Web.config and it already uses test connections

In the Web.Release.config, you simply have the namespace in the configuration tag and then you make any customizations you need to make the connection string live.
Code:
<?xml version="1.0"?>
<configuration  xmlns:xdt="[URL unfurl="true"]http://schemas.microsoft.com/XML-Document-Transform">[/URL]
    <connectionStrings>
        <add name="SQLServerConnectionString"
           connectionString="Server=LiveServer;Initial Catalog=LiveDB;User ID=USERID;Password=PASSWORD"
           providerName="System.Data.SqlClient"
		   xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
    </connectionStrings>
</configuration>

When you publish in Release mode, the example above will use the base web.config, then modify this connection string to produce the published web.config.

You can read more here: msdn - config transforms. There are a lot of fancier things you can do than the quick example I put here.

One thing to note... the transform only happens when you publish. Hitting F5 in Visual Studio will use the base web.config in my experience.

Best of luck! I hope this helps out.

-Mark
 
One more thought... If you're going to use debug/release configurations, you can also use compiler directives to display on screen whether you're in test or live. Of course, that doesn't have anything to do with the folder it's in or server it is on, just the configuration that was used to publish... but if your standard operating procedure has you publish debug to test and release to live, it will coincidentally work.

Code:
#if (DEBUG)
     Session[WhereAmI"] = "TEST";
#else
     Session[WhereAmI"] = "Production";
#endif

-Mark
 
Mark's ideas are the best route to go. Look at the article he posted
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top