Modify web.config at runtime in a pre-compiled site RRS feed

  • Question

  • User-1186634366 posted

    Hello, i'm developing an e-commerce free engine and i need to resolve one final thing...

    in my admin panel i could modify web.config for point on a theme specified by user...

    When save web.config it save correctly the new information, but the theme continue to point to the old value...

    It run if i use a updatable site but wannot run in a pre-compiled site... the same things appen on local IIS system.... in visual sudio it run...so i would to ask you the reason of this behaviour...

    I read online web.config in a pre-compiled site are not compiled and remain as clear text... so it could be modified...is right ? Why it don't run in my case ? What i wrong ?

    Thanks for your time!

    Friday, May 10, 2019 4:28 PM

All replies

  • User-943250815 posted

    Can not help with pre-compiled site.
    But, have in mind, any change on running web.config will reset/restart your Application Pool

    Friday, May 10, 2019 11:31 PM
  • User-1186634366 posted

    Thanks for your answer... i know web.config modification cause application restart... but i thought it did not influence the correct code execution...

    If i download web.config after the modification from admin panel, it have the new value for the theme i specified... but it wannot run on the new theme value but on old theme value.

    Someone on the net say could help to use external configuration file for values which need to be updated ...

      <appSettings configSource="appSettings.config"/>

    ...but the theme values are set in the web.config and is because of this i change directly this file.

    Someone have an idea for bypass this problem ?

    Saturday, May 11, 2019 5:04 AM
  • User-943250815 posted

    As I know you can move sections of web.config to another file, this just give you a way to organize data in separated files.
    I think you have 2 choices to store/retrieve themes:
    a) If you are using Membership or Identity, set Theme value on User Profile
    b) Store Theme value in a separated file, say an XML or TXT

    I have to correct myself, a change in web.config will "Shutdown" you site until first access.
    Here you can find a list of Application Shutdown Reason

    In Application_End (global.asax) you can check ApplicationShutdownReason and also provide a way to force your site to start again just calling a file


    Saturday, May 11, 2019 1:01 PM
  • User-1186634366 posted

    Thank you Jzero,

    i wannot site shutdown or similar, so i'm intentioned to move values i have to change from web.config to another external file...for example on appsettings.config.

    Do you think if i do this i could update the values and use immediately on the fly at runtime different as when in web.config ?

    In your idea is best to move on a external XML file ? Could i take somes advantages with .XML rather than .CONFIG file ? Is not more secure to store on .config file beacuse of xml can be edited ??

    Saturday, May 11, 2019 2:29 PM
  • User-943250815 posted

    Just store your file in App_Data folder, it will be in a safe place

    Saturday, May 11, 2019 6:58 PM
  • User-1186634366 posted

    Ok jzero, good advice store in App_data for major security, thanks !

    Another question please... for what i understand i have to set every section of webconfig in a different external file....

    ...for example to store pages configuration in web.config i set:

    <pages configSource="App_Data\PagesSettings.config"/>

    and in the external file i set:

    <?xml version="1.0" encoding="utf-8"?>
        <pages controlRenderingCompatibilityVersion="4.0" masterPageFile="~/masterpage.Master" theme="defaulttheme" clientIDMode="AutoID">
            <add tagPrefix="ajax" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
            <add tagPrefix="ajaxToolkit" assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" />

    and for AppSettings i have to do the same in a different external file...

    ...is that correct or can i do all in the same file ? I ask because Visual Studio tell me the external file have to init with page seciont tag...

    Last things.... if i use this way with separated file located in App_data, could i be sure that at runtime user could change values without shutdown site application ?

    Sunday, May 12, 2019 7:21 AM
  • User-943250815 posted

    @rava, let step back a little bit.
    Switching between themes is nothing more than set Page.Theme on page PreInit Event, on the other hand to persist Theme user choice, I just need store User/Theme in web.config as "key" in appSettings or in a database table, text file, xml file, json file to retrieve values when needed.
    Here a sample on how to switch between themes https://docs.microsoft.com/en-us/previous-versions/aspnet/tx35bd89(v=vs.100)

    Back to web.config and external files (sections), take a look here, specially Tip#8 and #10
    Changing Configuration in External File Example: https://docs.microsoft.com/en-us/previous-versions/ms228057(v=vs.140)
    Managing Changes to Configuration Settings: https://docs.microsoft.com/en-us/previous-versions/ms228058%28v%3dvs.140%29

    Suggest you not only check links above, but explore others links on each page, there are valuable info about, also look for "restartOnExternalChanges" option

    So you can do it easy way, just storing Theme & User in a single file on App_Data, or deal with web.config
    If you go with file in App_Data, give Read/Write permission to Application Pool User

    Sunday, May 12, 2019 12:43 PM
  • User-1186634366 posted

    Hi Jzero,

    i read your suggested articles.

    I moved all keys in separated .config files... all keys values are on AppSettings.config, other keys are in specific .config files... when updating no problem appear...it's good.

    I moved also theme value (masterpagefile value, theme name etc) in AppSettings.config so is sufficient (as you suggest) to update somes keys value and all work.

    I have a question for save config...

    I use this code for save

     Dim config As Configuration
    config = WebConfigurationManager.OpenWebConfiguration("~")

    ...set values keys ....


    With this code i hope to save all .config files related to web.config. Is this approach correct ?

    On Pre_Init i use this code for load correct masterpage.

       page.MasterPageFile = ConfigurationManager.AppSettings("Masterpagefile")
    page.Theme = ConfigurationManager.AppSettings("ThemeName")

    On the ASPX file i have this first line for call the masterpage

    <%@ Page Language="VB" MasterPageFile="~/hitech.master" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" EnableTheming="true" EnableViewState ="true" %>

    Because of this i have to set both masterpagegile and theme for run exactly as i want.

    Thanks for your help!!!

    Sunday, May 12, 2019 5:13 PM
  • User-943250815 posted

    Seems fine

    Sunday, May 12, 2019 11:48 PM
  • User-1186634366 posted
    A little issue of my approach. .. maybe if i change sessionstate value And debug boolean value cause restart of app though i have separated config Files And savemodeminimal option?
    Monday, May 13, 2019 4:52 AM
  • User-943250815 posted

    For sure it will restart.
    Better use global.asax Application_Start, Application_End to log events, so you know when shutdown/restart happens.
    I already posted a link with ShutdownReason, review it

    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs on application startup
      End Sub
    Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
      ' Code that runs on application shutdown
      Dim zShutdownReason As ApplicationShutdownReason = Hosting.HostingEnvironment.ShutdownReason
      Dim zReason As String = zShutdownReason.ToString
      gWriteToLog("Application_End_ShutDown_Reason:" & zReazon)                                        
    End Sub
    Private Sub gWriteToLog(ByVal pEvent As String) Dim zDirBase As String = HttpRuntime.AppDomainAppPath Dim zDirLog As String = IO.Path.GetFullPath(zDirBase & "\log\") Dim zLogFile As String = zDirLog & "MyApplication.log" ' Dim zDH As String = Now.ToString("yyyy-MM-ddTHH:mm:ss.fffzzz")
    Using sw As IO.StreamWriter = IO.File.AppendText(zLogFile) ' sw.WriteLine(zDH & " > " & pEvent) End Using End Sub

    Monday, May 13, 2019 12:44 PM
  • User753101303 posted


    You can also change the theme on the fly (or be more explicit about the exact change) : see https://benfoster.io/blog/applying-a-theme-programatically-in-aspnet-web-forms

    (sorry I skipped two quickly from the beginning to the end and see it has been suggested already). Tough I'm not sure what you are trying to solve you could also consider to NOT save this value to a config file.

    IMHO it could be considered as an app configuration value stored on the db side rarther than as really a config value (for example if you copy the db to another site you do expect to have the correct theme without having to separatly change the site config).

    Monday, May 13, 2019 12:59 PM
  • User-1186634366 posted

    Thank you PatriceSc,

    i'm saving all in config file because minor resources needed respect to DB loading ... and also because is simple to use.

    At now my problem is with somes value which seem to reload application (debug and sessiontimeout).... as you suggest, i could not include this modification and the problem could solve.

    I would also know if this code are correct for save all config external file called from web.config

    Dim config As Configuration
     config = WebConfigurationManager.OpenWebConfiguration("~")
    ...set values keys ....

    is save event propagate to external file automatically from the web.config were a link this external config files ?

    Tuesday, May 14, 2019 3:53 PM
  • User-1174608757 posted

    Hi rava,

    is save event propagate to external file automatically from the web.config were a link this external config files ?

    So you mean you want to  add link as variable in web.config file which shows as below:

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

    It works , but I suggest that you'd better not save all in config files,even though you could improve the speed but it will increase server pressure and when values in web.config is changed, application pool will reload the appplication which means session or other temporary values will lose.

    Best Regards


    Thursday, May 16, 2019 8:32 AM