var configPath=@"\\server\directory$\app\wwwroot\web.config";
if(System.IO.File.Exists(configPath)==false)
{
"Failed to find".Dump(configPath);
return;
}
var x=System.Xml.Linq.XDocument.Load(configPath);
//x.DumpFormatted();
var systemWeb=x.Root.XPathSelectElement("//system.web");
if(systemWeb==null)
{
x.DumpFormatted("no system.web");
return;
}
var ce=systemWeb.XPathSelectElements("customErrors");
if(ce.Count()>1)
{
ce.Skip(1).Remove();
x.Save(configPath);
}
if(ce!=null && ce.Any())
{
var customErrors=ce.First();
customErrors.Attributes(XNamespace.None+"mode").Dump("mode is already");
ce.Dump("already exists");
return;
}
systemWeb.AddFirst(
new XElement(systemWeb.Name.Namespace+"customErrors",
new XAttribute(XNamespace.None+"mode","Off")));
x.Save(configPath);
x=XDocument.Load(configPath);
x.Root.XPathSelectElement("//system.web/customErrors").Dump("changed to");
dmFyIGNvbmZpZ1BhdGg9QCJcXHNlcnZlclxkaXJlY3RvcnkkXGFwcFx3d3dyb290XHdlYi5jb25maWciOwoKaWYoU3lzdGVtLklPLkZpbGUuRXhpc3RzKGNvbmZpZ1BhdGgpPT1mYWxzZSkKewoJIkZhaWxlZCB0byBmaW5kIi5EdW1wKGNvbmZpZ1BhdGgpOwpyZXR1cm47Cn0KdmFyIHg9U3lzdGVtLlhtbC5MaW5xLlhEb2N1bWVudC5Mb2FkKGNvbmZpZ1BhdGgpOwoKLy94LkR1bXBGb3JtYXR0ZWQoKTsKdmFyIHN5c3RlbVdlYj14LlJvb3QuWFBhdGhTZWxlY3RFbGVtZW50KCIvL3N5c3RlbS53ZWIiKTsKaWYoc3lzdGVtV2ViPT1udWxsKQp7Cgl4LkR1bXBGb3JtYXR0ZWQoIm5vIHN5c3RlbS53ZWIiKTsKCXJldHVybjsKfQp2YXIgY2U9c3lzdGVtV2ViLlhQYXRoU2VsZWN0RWxlbWVudHMoImN1c3RvbUVycm9ycyIpOwppZihjZS5Db3VudCgpPjEpCnsKCWNlLlNraXAoMSkuUmVtb3ZlKCk7Cgl4Ll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