.net – Visual Studio Incorrectly Modifying web.config on publish


I’m having an issue where visual studio is modifying my web.config for encoded values on publish, but not on build, which wouldn’t be a major issue except it’s incorrectly changing them. Specifically, I need a tab character and it’s changing to a space, but I would really prefer it not change anything on a publish, especially if it didn’t change it when running the app locally.

I am running VS 17.12.4 using .net framework 4.8.09032.

I can recreate this by doing the below…

  1. create new application – ASP.NET Web Application (.NET Framework) – select Web API, leave all other settings at default
  2. add the below to appSettings in Web.Config
    <add key="TabValue1" value="&#9;"/>
    <add key="TabValue2" value="&#09;"/>
    <add key="LTValue1" value="&lt;"/>
    <add key="LTValue2" value="&#60;"/>
  1. Delete the Values controller
  2. Create a new controller and add the below
    [HttpGet]
    public HttpResponseMessage Get()
        => Request.CreateResponse(HttpStatusCode.OK, new
        {
            TabValue1 = ConfigurationManager.AppSettings["TabValue1"],
            TabValue2 = ConfigurationManager.AppSettings["TabValue2"],
            LTValue1 = ConfigurationManager.AppSettings["LTValue1"],
            LTValue2 = ConfigurationManager.AppSettings["LTValue2"]
        });
  1. Create a new folder publish profile using Debug configuration
  2. Publish the api
  3. Run the api in localhost

Hitting the new endpoint on local host yields the below as expected, and the config file in the bin looks the same as the Web.config.

{
"TabValue1": "\t",
"TabValue2": "\t",
"LTValue1": "<",
"LTValue2": "<" 
}

However, the web.config file in the publish directory looks like the below.

<add key="TabValue1" value=" " />
<add key="TabValue2" value=" " />
<add key="LTValue1" value="&lt;" />
<add key="LTValue2" value="&lt;" />

Leave a Reply

Your email address will not be published. Required fields are marked *