Changes to a web.config file of Web application project before deployment to a production environment

Donald Symmons 3,091 Reputation points
2023-02-12T15:59:00.6166667+00:00

I just recently discovered that there are differences in the web.config file of a development environment to that of a production environment. I was reading articles about this and did not fully understand it.

web deployment

https://learn.microsoft.com/en-us/previous-versions/dd465326(v=vs.100)?redirectedfrom=MSDN

https://learn.microsoft.com/en-us/aspnet/web-forms/overview/older-versions-getting-started/deploying-web-site-projects/common-configuration-differences-between-development-and-production-cs

I thought that when using the “Publish” option in “Build” menu, it will automatically do the necessary changes in the web.config (like changing the compilation debug="true"), apart from the connection string, mail settings and SessionState.

In one of the articles, I read a line where it is stated that Visual Studio can automatically do the necessary changes when we use the publish tool from VS, please, is that true?

I will please like to get a clarity on this and also, if possible, a brief step-by-step direction on the things that needs to be done in my own web.config file?

Thank you.

Here is a sample of my ASP.NET Web Application web.config


<configuration>
  <connectionStrings>
    <add name="ConString" connectionString="MyConnectionString" />
  </connectionStrings>
  <appSettings>
    <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
  </appSettings>
  <system.web>
    <sessionState timeout="120"></sessionState>
    <authentication mode="Forms">
      <forms name="login" timeout="120" cookieless="UseCookies" loginUrl="Login.aspx" slidingExpiration="true" />
    </authentication>
    <authorization>
      <deny users="?"/>
    </authorization>
    <compilation debug="true" targetFramework="4.7.2" />
    <httpRuntime targetFramework="4.7.2" maxRequestLength="3145728" />
    <customErrors mode="Off" />
  <pages enableEventValidation="false">
      <controls>
        <add tagPrefix="ajaxToolkit" assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" />
      </controls>
    </pages>
  </system.web>
  <system.net>
    <mailSettings>
      <smtp deliveryMethod="Network" from="******@mymail.com">
        <network host="smtp.secureserver.net" port="20" enableSsl="false" userName="******@mymail.com" password="password" defaultCredentials="false" />
      </smtp>
    </mailSettings>
  </system.net>
  <system.web.extensions>
    <scripting>
      <webServices>
        <jsonSerialization maxJsonLength="819200000">
        </jsonSerialization>
      </webServices>
    </scripting>
  </system.web.extensions>
  <location path="Signup.aspx">
    <system.web>
      <authorization>
        <allow users="*" />
      </authorization>
    </system.web>
  </location>
  <location path="Default.aspx">
    <system.web>
      <authorization>
        <allow users="*" />
      </authorization>
    </system.web>
  </location>
  <system.codedom>
    <compilers>
      <compiler extension=".cs" language="c#;cs;csharp" warningLevel="4" compilerOptions="/langversion:7.3 /nowarn:1659;1699;1701;612;618" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=3.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
      <compiler extension=".vb" language="vb;vbs;visualbasic;vbscript" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008,40000,40008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=3.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    </compilers>
  </system.codedom>
</configuration>

Microsoft Security | Intune | Configuration Manager | Deployment
Developer technologies | ASP.NET | Other
0 comments No comments
{count} votes

Answer accepted by question author
  1. Lan Huang-MSFT 30,206 Reputation points Microsoft External Staff
    2023-02-13T08:09:51.1733333+00:00

    Hi @Donald Symmons ,

    In one of the articles, I read a line where it is stated that Visual Studio can automatically do the necessary changes when we use the publish tool from VS, please, is that true?

    This is true.

    There are two ways to automate the process of changing Web.config file settings: Web.config transformations and Web Deploy parameters.

    After creating a publish profile and deploying the database, you can configure the publish profile transformation.

    There are detailed steps to deploy to production environment in the document. You can also try deploying to a test environment first.

    ASP.NET Web Deployment using Visual Studio: Deploying to Production

    ASP.NET Web Deployment using Visual Studio: Deploying to Test

    Configure the Publish Profile Transformation Steps:

    1. In Solution Explorer, expand Properties, expand PublishProfiles.
    2. Right-click CustomProfile.pubxml and rename it Test.pubxml.
    3. Right-click Test.pubxml. Select Add Config Transform.(Visual Studio creates the Web.Test.config transform file and opens it.)
    4. In the Web.Test.config transform file, insert the following code immediately after the opening configuration tag.
          <appSettings>
              <add key="Environment" value="Test" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
          </appSettings>
      
    5. Save and close the file.
    6. Right-click the Web.Test.config file and select Preview Transform to make sure that the transform you coded produces the expected changes. The Web.config Preview window shows the result of applying both the Web.Release.config transforms and the Web.Test.config transforms.

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.