Back-End Development

Dynamically Set Connection Strings in Web.Config

1600x600 Keyboard

If you are struggling to set up the web.config files upon hosting your web application, here is a solution to how you can configure your Web.Debug.config and Web.Release.config file so that your application runs dynamically on both the dev and prod environments.

 

Web.Config

Leave the original web.config file as it is. Make sure you have your RedirectUri configured as well. Click here to see how you can manually set that up if your hosted site directs to your localhost upon authentication.

B6 P1

 

Web.Debug.Config

Add the keys for Environment(default) and Connection String in the Web.Debug.config file.

  • xdt:Locator=”Match(key)” indicates that we want to match with the key variable.
  • xdt:Transforms=”SetAttributes” allows us to set the corresponding key with the value we have assigned here. You can also “Replace”, “Insert”, or “RemoveAttributes()”. Click here for more syntax.

B6 P2

Covid 19
COVID-19: Digital Insights For Enterprise Action

Access Perficient’s latest insights into how you can leverage digital technologies to not only respond to the pandemic, but drive your operations forward and deliver experiences your customers need.

Get Informed

 

Web.Release.Config

Same logic applies to the Web.Release.Config file. Here is where you manage your configurations for your hosted site. Set the attributes for your Environment (default), ClientId, RedirectUri, PostLogoutRedirectUri, and ConnectionString.

B6 P3

 

Where Do I Find my Client ID and Connection String?

In case you are confused on where you can retrieve your ClientId or the ConnectionString for your Azure SQL, refer below.

In your Azure Portal, go to Azure Active Directory > App Registrations > Corresponding Web App to get your ClientId for the hosted web application.

B6 P4

SQL databases > Connection Strings > SQL Authentication

B6 P5

 

Hope that helped!

About the Author

David Hwang is an Associate Technical Consultant in the Sitecore/Microsoft Business Unit. He will be blogging about various concepts in technologies for beginners.

More from this Author

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe to the Weekly Blog Digest:

Sign Up