[英]Relative and absolute paths on ASP.NET/IIS
I've read many articles about relative/absolute paths, but I still can't grok this problem. 我读过很多关于相对/绝对路径的文章,但我仍然无法解决这个问题。
The following code is from my ASP.NET Master
page: 以下代码来自我的ASP.NET Master
页:
<li><a>Reports</a>
<ul>
<li>
<a href="/Reports/One.aspx">One</a>
</li>
<li>
<a href="~/Reports/Two.aspx">Two</a>
</li>
</ul>
</li>
(Note that one link has a ~
and one doesn't.) (注意,一个链接有~
而一个没有。)
When running the site, the first link points to http://server/Reports/One.aspx
, and the second link points to http://server/company/project/Reports/~/Reports/Two.aspx
. 运行站点时,第一个链接指向http://server/Reports/One.aspx
,第二个链接指向http://server/company/project/Reports/~/Reports/Two.aspx
。
How do I get to the root of my ASP.NET project without it ignoring whatever virtual directories are set up on IIS? 如何在不忽略IIS上设置的虚拟目录的情况下访问ASP.NET项目的根目录?
Add runat="server"
attribute to the anchor tag. 将runat="server"
属性添加到锚标记。 You can't use the ~ root operator with HTML tags. 您不能将~root运算符与HTML标记一起使用。 Only the server controls (Html or Web) can use it. 只有服务器控件(Html或Web)才能使用它。
<a runat="server" href="~/Reports/Two.aspx">Two</a>
如果您不希望它们成为具有生成的ID的服务器控件,请将Page.ResovleUrl
用于所有文件:
<a href='<%= Page.ResolveUrl("~/Reports/Two.aspx")%>'>Two</a>
A relative path is relative to the current resource, so if you were viewing 相对路径是相对于当前资源的,因此如果您正在查看
http://yourhost/app/default.aspx
a relative path of reports/one.aspx
would be http://yourhost/app/reports/one.aspx
. reports/one.aspx
的相对路径为http://yourhost/app/reports/one.aspx
。 Note the absence of a leading /
in the relative path. 注意相对路径中没有前导/
。 That's what makes it relative. 这就是它相对的原因。
An absolute path, as you can probably guess, starts with a /
, and it uses the hostname of the current resource, so that would http://yourhost/reports/one.aspx
. 您可以猜测,绝对路径以/
开头,它使用当前资源的主机名,因此http://yourhost/reports/one.aspx
。
~ is an red herring. 〜是一只红鲱鱼。 It's a .NET -only addition used by various parts of ASP.NET to base your path off the current application root. 它是一个.NET,它只是ASP.NET的各个部分用来基于当前应用程序根目录的路径。 So if your application root was http://yourhost/app
, you were viewing http://yourhost/app/views/default.aspx
, and you asked .NET for the path ~/reports/one.aspx', you would be given
http://yourhost/app/reports/one.aspx `. 因此,如果您的应用程序根目录是http://yourhost/app
,那么您正在查看http://yourhost/app/views/default.aspx
,并且您要求.NET提供路径~/reports/one.aspx', you would be given
http://yourhost/app/reports/one.aspx` 。
~ isn't used by HTML, IIS or URLs, so if your browser sees it, it'll just use it as is. 〜不是HTML,IIS或URL使用的,所以如果你的浏览器看到它,它就会按原样使用它。
Note : Some Unix servers can use ~
to map in a user's home directory, but that's just complicating things. 注意 :有些Unix服务器可以使用~
来映射用户的主目录,但这只会让事情复杂化。
Please read There is something about "Paths" for ASP.NET beginners . 请阅读有关ASP.NET初学者的“路径”的内容 。 It will give a complete idea on "Paths" in an ASP.NET application. 它将给出ASP.NET应用程序中“路径”的完整概念。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.