locked
Save URL in Webconfig? RRS feed

  • Question

  • User306743125 posted

    Hi 

    General version of my question:

    I have a URL I often refer to in my Code.
    Is it possible to store this URL in my webconfig and then refer to the URL by the name given in the webconfig (like we do with a connectionstring)?
    That would mean the URL is stored in a central location and I would not have to keep repeating it in my code.

    My specific situation:

    I use Azure blob storage.
    In my code I refer to the URL of the Blob Container and dynamically pass the specific image's filename.
    Especially when I flip between Live and Testing, I have to keep changing all the URLs in my code from the actual URL of my Azure blob to the localhost url of the storage emulator.

    Is it possible to save the Blob's URL in the Webconfig and refer to that in my code?
    At least then I would only 1 url to update when doing testing.

    I guess I would like to do something like this in the webconfig:

    <add name="BlobUrl" url="http://myblobstorage.blob.core.windows.net/images/">

    Wednesday, April 9, 2014 4:25 PM

Answers

  • User-760709272 posted

    In web.config

    <?xml version="1.0"?>
    <configuration>
      <appSettings>
        <add key="BlobUrl" value="http://myblobstorage.blob.core.windows.net/images/"/>
      </appSettings>

    In code

    string url = System.Configuration.ConfigurationManager.AppSettings["BlobUrl"];

    A further trick you can do is automatically change it when you publish a certain version (debug, release, or you could create a test config etc) is to use transforms.  If you only have web.config and not web.release.config etc then right click on web.config and select "Add transforms".  In the release version, web.release.config

    <?xml version="1.0"?>
    
    <!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->
    
    <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
      <appSettings>
        <add key="BlobUrl" value="http://LIVE.myblobstorage.blob.core.windows.net/images/"
             xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
      </appSettings>

    Now when you publish in Release it will transform the value to the copy in the release version instead.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 9, 2014 5:53 PM

All replies

  • User-760709272 posted

    In web.config

    <?xml version="1.0"?>
    <configuration>
      <appSettings>
        <add key="BlobUrl" value="http://myblobstorage.blob.core.windows.net/images/"/>
      </appSettings>

    In code

    string url = System.Configuration.ConfigurationManager.AppSettings["BlobUrl"];

    A further trick you can do is automatically change it when you publish a certain version (debug, release, or you could create a test config etc) is to use transforms.  If you only have web.config and not web.release.config etc then right click on web.config and select "Add transforms".  In the release version, web.release.config

    <?xml version="1.0"?>
    
    <!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->
    
    <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
      <appSettings>
        <add key="BlobUrl" value="http://LIVE.myblobstorage.blob.core.windows.net/images/"
             xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
      </appSettings>

    Now when you publish in Release it will transform the value to the copy in the release version instead.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 9, 2014 5:53 PM
  • User306743125 posted

    Thank you ... that is helpfull

    Thursday, April 10, 2014 9:41 AM