Skip to content

Commit

Permalink
Merge pull request dotnet#1425 from dotnet/vramak/ConsistentWebConfig…
Browse files Browse the repository at this point in the history
…Update

Consistent Web.Config update experience
  • Loading branch information
vijayrkn committed Apr 29, 2020
2 parents 4a4ae15 + 11aea5e commit 840b1c9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
32 changes: 25 additions & 7 deletions src/Publish/Tasks/Tasks/TransformWebConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,34 @@ public override bool Execute()
Log.LogMessage(MessageImportance.Low, $"Configuring the following project for use with IIS: '{PublishDir}'");

XDocument webConfigXml = null;
string webConfigPath = Path.Combine(PublishDir, "web.config");
webConfigPath = Path.GetFullPath(webConfigPath);

if (File.Exists(webConfigPath))
// Initialize the publish web.config file with project web.config content if present. Else, clean the existing web.config in the
// publish folder to make sure we have a consistent web.config update experience.
string projectWebConfigPath = null;
if (!string.IsNullOrEmpty(ProjectFullPath))
{
Log.LogMessage($"Updating web.config at '{webConfigPath}'");
projectWebConfigPath = Path.Combine(Path.GetDirectoryName(ProjectFullPath), "web.config");
}

string publishWebConfigPath = Path.Combine(PublishDir, "web.config");
publishWebConfigPath = Path.GetFullPath(publishWebConfigPath);

if (File.Exists(publishWebConfigPath))
{
if (File.Exists(projectWebConfigPath))
{
File.Copy(projectWebConfigPath, publishWebConfigPath, true);
}
else
{
File.WriteAllText(publishWebConfigPath, WebConfigTemplate.Template);
}

Log.LogMessage($"Updating web.config at '{publishWebConfigPath}'");

try
{
webConfigXml = XDocument.Load(webConfigPath);
webConfigXml = XDocument.Load(publishWebConfigPath);
}
catch (XmlException e)
{
Expand All @@ -90,7 +108,7 @@ public override bool Execute()
}
else
{
Log.LogMessage($"No web.config found. Creating '{webConfigPath}'");
Log.LogMessage($"No web.config found. Creating '{publishWebConfigPath}'");
}

if (IsAzure)
Expand All @@ -112,7 +130,7 @@ public override bool Execute()

// Telemetry
transformedConfig = WebConfigTelemetry.AddTelemetry(transformedConfig, ProjectGuid, IgnoreProjectGuid, SolutionPath, ProjectFullPath);
using (FileStream f = new FileStream(webConfigPath, FileMode.Create))
using (FileStream f = new FileStream(publishWebConfigPath, FileMode.Create))
{
transformedConfig.Save(f);
}
Expand Down
7 changes: 7 additions & 0 deletions src/Publish/Tasks/WebConfigTemplate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Microsoft.NET.Sdk.Publish.Tasks
{
public static class WebConfigTemplate
{
public const string Template = @"<configuration><location path=""."" inheritInChildApplications=""false"" /></configuration>";
}
}
4 changes: 1 addition & 3 deletions src/Publish/Tasks/WebConfigTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ public static XDocument Transform(XDocument webConfig, string appName, bool conf
const string aspNetCoreElementName = "aspNetCore";
const string envVariablesElementName = "environmentVariables";

const string WebConfigTemplate = @"<configuration><location path=""."" inheritInChildApplications=""false"" /></configuration>";

if (webConfig == null || webConfig.Root.Name.LocalName != "configuration")
{
webConfig = XDocument.Parse(WebConfigTemplate);
webConfig = XDocument.Parse(WebConfigTemplate.Template);
}

XElement rootElement = null;
Expand Down

0 comments on commit 840b1c9

Please sign in to comment.