Sunday, June 1, 2014

Mapping virtual path to physical path using Server.MapPath method

Server.MapPath() returns the physical path for a given virtual path. This method can be used in several different ways, depending on the characters that we passed into this method.
Server.MapPath(“.”) --> Current physical directory of the page that you are running
Server.MapPath(“..”)--> Parent physical directory of the page that you are running
Server.MapPath(“~”) --> The physical path of the root directory of the application.
Example:
PageInElectronicFolder.aspx :-
 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="PageInElectronicFolder.aspx.cs" Inherits="_19_MappingVirtualpath_Physicalpath.Categories.Electronics.PageInElectronicFolder" %>  
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
 <html xmlns="http://www.w3.org/1999/xhtml">  
 <head runat="server">  
   <title></title>  
 </head>  
 <body>  
   <form id="form1" runat="server">  
   <div>  
     <asp:DropDownList ID="DropDownList1" runat="server">  
     </asp:DropDownList>  
   </div>  
   </form>  
 </body>  
 </html>  

PageInElectronicFolder.aspx.cs :-
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Web;  
 using System.Web.UI;  
 using System.Web.UI.WebControls;  
 using System.Data;  
 namespace _19_MappingVirtualpath_Physicalpath.Categories.Electronics  
 {  
   public partial class PageInElectronicFolder : System.Web.UI.Page  
   {  
     protected void Page_Load(object sender, EventArgs e)  
     {  
       DataSet DS = new DataSet();//To read the xml file  
       string strPhysicalPath = Server.MapPath("~/Data/Countries/Countries.xml");// "~/Data/Countries/Countries.xml" is a virtual path  
                                          // "~" is used to get the root directory of the Application project  
       DS.ReadXml(strPhysicalPath);  
       DropDownList1.DataTextField = "CountryName";  
       DropDownList1.DataValueField = "CountryId";  
       DropDownList1.DataSource = DS;  
       DropDownList1.DataBind();  
       ListItem li = new ListItem("Select", "-1");  
       //DropDownList1.Items.Add(li);  
       DropDownList1.Items.Insert(0, li);  
     }  
   }  
 }  

Countries.xml :-
 <?xml version="1.0" encoding="utf-8" ?>  
 <Countries>  
  <Country>  
   <CountryId>101</CountryId>  
   <CountryName>London</CountryName>  
  </Country>  
  <Country>  
   <CountryId>102</CountryId>  
   <CountryName>Canada</CountryName>  
  </Country>  
  <Country>  
   <CountryId>103</CountryId>  
   <CountryName>Australiya</CountryName>  
  </Country>  
  <Country>  
   <CountryId>104</CountryId>  
   <CountryName>India</CountryName>  
  </Country>  
 </Countries>  

PageOnRootDirectory.aspx :-
 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="PageOnRootDirectory .aspx.cs" Inherits="_19_MappingVirtualpath_Physicalpath.Page" %>  
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
 <html xmlns="http://www.w3.org/1999/xhtml">  
 <head runat="server">  
   <title></title>  
 </head>  
 <body>  
   <form id="form1" runat="server">  
   <div>  
     <asp:DropDownList ID="DropDownList1" runat="server">  
     </asp:DropDownList>  
   </div>  
   </form>  
 </body>  
 </html>  

PageOnRootDirectory.aspx.cs :-
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Web;  
 using System.Web.UI;  
 using System.Web.UI.WebControls;  
 using System.Data;  
 namespace _19_MappingVirtualpath_Physicalpath  
 {  
   public partial class Page : System.Web.UI.Page  
   {  
     protected void Page_Load(object sender, EventArgs e)  
     {  
       DataSet DS = new DataSet();  
       string strPhysicalPath = Server.MapPath("~/Data/Countries/Countries.xml");  
       DS.ReadXml(strPhysicalPath);  
       DropDownList1.DataTextField = "CountryName";  
       DropDownList1.DataValueField = "CountryId";  
       DropDownList1.DataSource = DS;  
       DropDownList1.DataBind();  
       ListItem li = new ListItem("Select", "-1");  
       //DropDownList1.Items.Add(li);  
       DropDownList1.Items.Insert(0, li);  
     }  
   }  
 }  


If the PageInElectronicFolder.aspx.cs has to find the Countries.xml file It has to navigate the root directory from there go to the Data directory and then to the Countries directory. If we use “../../Data/Countries/Countries.xml” for the virtual path then there is problem…..because we copy the coding from PageInElectronicFolder.aspx.cs to PageOnRootDirectory .aspx.cs then it won’t work for that we use “~”.This symbol resolves to the root application directory, no matter how deep you are in the folder hierarchy. The following code will work from any folder in the application. DS.ReadXml(Server.MapPath("~/Data/Countries/Countries.xml"));

No comments:

Post a Comment