Contacts

Managing two websites with seperate master page files and development environments.

I came across a situation the other day where I was developing two websites that used the same base code, but differentiated by masterpage files and a few settings in the web.config to make calls to web services and databases. Our initial development had been hurried, so everytime we rolled to production, we changed the masterpage file, and the settings by commenting out the sections of the web.config. When I finally got sick of doing this, I started writing code to handle this. First, I uncommented the lines in the web.config, and added prefixes to the key names.
  1. <appsettings>
  2.     <!– Production Settings –>
  3.     <add key="prodSetting1" value="prodValue1"></add>
  4.     <add key="prodSetting2" value="prodValue2"></add>
  5.     <add key="prodSetting3" value="prodValue3"></add>
  6.  
  7.     <!– QA Settings –>
  8.     <add key="qaSetting1" value="qaValue1"></add>
  9.     <add key="qaSetting2" value="qaValue2"></add>
  10.     <add key="qaSetting3" value="qaValue3"></add>
  11.  
  12.     <!– Site 1 Settings –>
  13.     <add key="site1MasterPageFile" value="~/site1.master"></add>
  14.     <add key="site1Theme" value="site1"></add>
  15.  
  16.     <!– Site 2 Settings –>
  17.     <add key="site2MasterPageFile" value="~/site2.master"></add>
  18.     <add key="site2Theme" value="site2"></add>
  19. </appsettings>
After that, we create a new class in the App_Code folder, which inherits System.Web.UI.Page
  1. Public Class MyCustomPage
  2.     Inherits System.Web.UI.Page
  3.     Public Setting1 As String
  4.     Public Setting2 As String
  5.     Public Setting3 As String
  6.  
  7.     Private Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
  8.         If Request.ServerVariables("HTTP_HOST").StartsWith("qa.") Then
  9.             Setting1 = System.Configuration.ConfigurationManager.AppSettings("qaSetting1")
  10.             Setting2 = System.Configuration.ConfigurationManager.AppSettings("qaSetting2")
  11.             Setting3 = System.Configuration.ConfigurationManager.AppSettings("qaSetting2")
  12.         Else
  13.             Setting1 = System.Configuration.ConfigurationManager.AppSettings("prodSetting1")
  14.             Setting2 = System.Configuration.ConfigurationManager.AppSettings("prodSetting2")
  15.             Setting3 = System.Configuration.ConfigurationManager.AppSettings("prodSetting2")
  16.         End If
  17.  
  18.         If Request.ServerVariables("HTTP_HOST").Contains("ryanmarrs.com") Then
  19.             Page.Theme = System.Configuration.ConfigurationManager.AppSettings("site1Theme")
  20.             Page.MasterPageFile = System.Configuration.ConfigurationManager.AppSettings("site1MasterPageFile")
  21.         Else
  22.             Page.Theme = System.Configuration.ConfigurationManager.AppSettings("site2Theme")
  23.             Page.MasterPageFile = System.Configuration.ConfigurationManager.AppSettings("site2MasterPageFile")
  24.         End If
  25.     End Sub
  26. End Class
After this, we change the inherits on our web pages to the following:
  1. Partial Public Class _Default
  2.     Inherits MyCustomPage
  3.  
  4.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  5.         me.Label1.text = Setting1
  6.         me.Label2.text = Setting2
  7.         me.Label3.text = Setting3
  8.     End Sub
  9. End Class
We’re done. You can also create inheritance for things like UserControls or MasterPage files if you need those settings inside them Keep in mind, usercontrols and master page files don’t get a theme or a master page file.
Filed under: