ASP.NET的“~”路径

   在ASP.NET里增加了一个新的表达方法“~”,“~”表示的路径是当前应用程序的跟目录。“~”和“/”最大的区别是由服务器进行动态解释。由于”~”是相对于应用程序的根目录,所以利用它可以简化路径的设置,在某些情况下似乎还必须使用该控件,例如如下使用了一个TreeView控件(具体以后会介绍)

<asp:TreeView ID="TreeView1"

          RootNodeStyle-ImageUrl="~/images/xp/computer.gif"

          ParentNodeStyle-ImageUrl="~/images/xp/folder.gif"

          LeafNodeStyle-ImageUrl="~/images/xp/ie.gif"

          CollapseImageUrl="~/images/xp/minus.gif"

          ExpandImageUrl="~/images/xp/plus.gif"

          NoExpandImageUrl="~/images/xp/blank.gif"

          runat="server">

          <Nodes>

            <asp:TreeNode Text="My Computer">

              <asp:TreeNode Text="Favorites" ImageUrl="~/images/xp/star.gif">

                <asp:TreeNode Text="News"> 

                  <asp:TreeNode Text="MSN" NavigateUrl="http://www.msn.com"/>

                  <asp:TreeNode Text="MSNBC News" NavigateUrl="http://www.msnbc.msn.com"/>

                </asp:TreeNode>

                <asp:TreeNode Text="Technology"> 

                  <asp:TreeNode Text="Microsoft" NavigateUrl="http://www.microsoft.com"/>

                  <asp:TreeNode Text="ASP.NET" NavigateUrl="http://www.asp.net"/>

                  <asp:TreeNode Text="GotDotNet" NavigateUrl="http://www.gotdotnet.com"/>

                  <asp:TreeNode Text="MSDN" NavigateUrl="http://msdn.microsoft.com"/>

                </asp:TreeNode>

                <asp:TreeNode Text="Shopping"> 

                  <asp:TreeNode Text="MSN Shopping" NavigateUrl="http://shopping.msn.com"/>

                  <asp:TreeNode Text="MSN Autos" NavigateUrl="http://autos.msn.com"/>

                </asp:TreeNode>

              </asp:TreeNode>

              <asp:TreeNode Text="City Links">

                <asp:TreeNode Text="MapPoint" NavigateUrl="http://www.mappoint.com"/>

                <asp:TreeNode Text="MSN City Guides" NavigateUrl="http://local.msn.com"/>

              </asp:TreeNode>

              <asp:TreeNode Text="Music Links">

                <asp:TreeNode Text="MSN Music" NavigateUrl="http://music.msn.com"/>

              </asp:TreeNode>

            </asp:TreeNode>

          </Nodes>

        </asp:TreeView>

 面显示了它的运行结果:

                              

                      图3-6 TreeView控件运行结果图

   在这个运行结果里可以看到News和CityLinks连接器都使用了文件夹图片folder.gif,这是通过

ParentNodeStyle-ImageUrl="~/images/xp/folder.gif"

进行设置的。在这里使用相对路径是不行的,因为在运行时,News和CityLinks在不同的级别,无法使用相对路径设置,如果使用绝对路径这里也算一个不错的选择,如果使用相对于根目录的路径,如果您使用的虚拟主机的话,就会发现有时你并得不到相对于根目录的路径,所以这里使用相对于应用程序的根目录确实是一个最好的做法。这个“~”就会由.NET在运行时自动解析的。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值