Get Physical Root

Get Physical Root

Map Path

  • Use the MapPath method to assign the physical file path of an item to a string
  • Sitecore compares case when evaluating item paths
  • Start file paths provided to the MapPath() method with a "/" or "~/" 
  • If the file path is blank, contains a backslash (\) or contains the string "://", the current path is returned instead

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Data.Items.Item
  • Sitecore.IO.FileUtil
  • System.Web.HttpContext

Physical Root C# Web Control Code

Sitecore.Data.Items.Item item = this.GetItem();
if (item == null) return;

var aspMapPath = new string[6];
aspMapPath[0] = System.Web.HttpContext.Current.Server.MapPath("/");
aspMapPath[1] = System.Web.HttpContext.Current.Server.MapPath("~");
aspMapPath[2] = System.Web.HttpContext.Current.Server.MapPath("");
aspMapPath[3] = System.Web.HttpContext.Current.Server.MapPath("/web.config");
aspMapPath[4] = System.Web.HttpContext.Current.Server.MapPath("~/web.config");
aspMapPath[5] = System.Web.HttpContext.Current.Server.MapPath("webconfig");

var apiMapPath = new string[6];
apiMapPath[0] = Sitecore.IO.FileUtil.MapPath("/");
apiMapPath[1] = Sitecore.IO.FileUtil.MapPath("~");
apiMapPath[2] = Sitecore.IO.FileUtil.MapPath("");
apiMapPath[3] = Sitecore.IO.FileUtil.MapPath("/web.config"); 
apiMapPath[4] = Sitecore.IO.FileUtil.MapPath("~/web.config"); 
apiMapPath[5] = Sitecore.IO.FileUtil.MapPath("web.config");

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.Write("{0}", "C# Web Control Real-time Output");
output.RenderEndTag();

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-size:x-small; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
//using : for colon and   for space
output.Write("{0}: {1}", "Item", item.DisplayName);
output.RenderEndTag(); //P

output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
output.Write("{0}", "Using Asp.Net");
output.RenderEndTag(); //P

output.Write("{0}&#58;&#160;{1}", @"System.Web.HttpContext.Current.Server.MapPath(""/"")", "<b>" + aspMapPath[0] + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", @"System.Web.HttpContext.Current.Server.MapPath(""~"")", "<b>" + aspMapPath[1] + "</b><br/>");
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "color:darkred");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Span);
output.Write("{0}&#58;&#160;{1}", @"System.Web.HttpContext.Current.Server.MapPath("""")", aspMapPath[2] + "<br/>");
output.RenderEndTag(); //Span
output.Write("{0}&#58;&#160;{1}", @"System.Web.HttpContext.Current.Server.MapPath(""/web.config"")", "<b>" + aspMapPath[3] + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", @"System.Web.HttpContext.Current.Server.MapPath(""~/web.config"")", "<b>" + aspMapPath[4] + "</b><br/>");
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "color:darkred");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Span);
output.Write("{0}&#58;&#160;{1}", @"System.Web.HttpContext.Current.Server.MapPath(""webconfig"")", aspMapPath[5]);
output.RenderEndTag(); //Span

output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
output.Write("{0}", "Using Sitecore API");
output.RenderEndTag(); //P

output.Write("{0}&#58;&#160;{1}", @"Sitecore.IO.FileUtil.MapPath(""/"")", "<b>" + apiMapPath[0] + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", @"Sitecore.IO.FileUtil.MapPath(""~"")", "<b>" + apiMapPath[1] + "</b><br/>");
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "color:darkred");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Span);
output.Write("{0}&#58;&#160;{1}", @"Sitecore.IO.FileUtil.MapPath("""")", apiMapPath[2] + "<br/>");
output.RenderEndTag(); //Span
output.Write("{0}&#58;&#160;{1}", @"Sitecore.IO.FileUtil.MapPath(""/web.config"")", "<b>" + apiMapPath[3] + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", @"Sitecore.IO.FileUtil.MapPath(""~/web.config"")", "<b>" + apiMapPath[4] + "</b><br/>");
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "color:darkred");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Span);
output.Write("{0}&#58;&#160;{1}", @"Sitecore.IO.FileUtil.MapPath(""webconfig"")", apiMapPath[5]);
output.RenderEndTag(); //Span

output.RenderEndTag(); //Div


Physical Root C# Web Control Output

Using Asp.Net

System.Web.HttpContext.Current.Server.MapPath("/"): C:\inetpub\wwwroot\TIC66Live\Website\
System.Web.HttpContext.Current.Server.MapPath("~"): C:\inetpub\wwwroot\TIC66Live\Website\
System.Web.HttpContext.Current.Server.MapPath(""): C:\inetpub\wwwroot\TIC66Live\Website\en\Developers\SitecoreSites\SiteManagement
System.Web.HttpContext.Current.Server.MapPath("/web.config"): C:\inetpub\wwwroot\TIC66Live\Website\web.config
System.Web.HttpContext.Current.Server.MapPath("~/web.config"): C:\inetpub\wwwroot\TIC66Live\Website\web.config
System.Web.HttpContext.Current.Server.MapPath("webconfig"): C:\inetpub\wwwroot\TIC66Live\Website\en\Developers\SitecoreSites\SiteManagement\webconfig

Using Sitecore API

Sitecore.IO.FileUtil.MapPath("/"): C:\inetpub\wwwroot\TIC66Live\Website\
Sitecore.IO.FileUtil.MapPath("~"): C:\inetpub\wwwroot\TIC66Live\Website\
Sitecore.IO.FileUtil.MapPath(""): 
Sitecore.IO.FileUtil.MapPath("/web.config"): C:\inetpub\wwwroot\TIC66Live\Website\web.config
Sitecore.IO.FileUtil.MapPath("~/web.config"): C:\inetpub\wwwroot\TIC66Live\Website\web.config
Sitecore.IO.FileUtil.MapPath("webconfig"): C:\inetpub\wwwroot\TIC66Live\Website\en\Developers\SitecoreSites\SiteManagement\web.config

Sources