为什么在重定向时需要提及http

时间:2011-05-28 09:05:29

标签: asp.net

我已经通过点击超链接

完成了重定向到谷歌的示例代码

这个工作得很好

<asp:HyperLink ID="MyHyperLinkControl" NavigateUrl="http://google.com""
    runat="server">link</asp:HyperLink>

但这不起作用

<asp:HyperLink ID="MyHyperLinkControl" NavigateUrl="www.google.com" 
    runat="server">link</asp:HyperLink>

任何人都可以给我详细解释

2 个答案:

答案 0 :(得分:3)

NavigateUrl="relative-path"转到./relative-path,这意味着如果您的浏览器目前位于http://www.example.com/test,则该链接会告诉它转到http://www.example.com/test/relative-path

因此,NavigateUrl="www.google.com"将转到http://www.example.com/test/www.google.com

但是,如果您指定了完整的网址,例如NavigateUrl="http://www.google.com/",那么您将转到http://www.google.com/


举例说明:

如果您的网页上有超链接,并且该网页位于http://www.example.com/test

example.html      goes to   http://www.example.com/test/example.html
example           goes to   http://www.example.com/test/example
google.html       goes to   http://www.example.com/test/google.html
www.google.html   goes to   http://www.example.com/test/www.google.html
www.google.com    goes to   http://www.example.com/test/www.google.com

它被称为www.google.com的事实使它与任何其他链接没有区别。

如果您使用绝对链接(以/开头的链接),您将获得以下行为:

/example.html     goes to   http://www.example.com/example.html
/example          goes to   http://www.example.com/example
/google.html      goes to   http://www.example.com/google.html
/www.google.html  goes to   http://www.example.com/www.google.html
/www.google.com   goes to   http://www.example.com/www.google.com

但是,如果您指定完整的URL(包括架构),则地址栏中的整个URL将替换为完整URL指向的URL。例如:

mailto:test@example.com  uses the email client
http://www.google.com    uses the browser, pointing it to that URL

答案 1 :(得分:0)

我猜它是因为它将网址视为相对网址,所以如果你当前在网页上:www.yourdomain.com/,第二个会尝试导航到www.yourdomain.com/www.google。 COM。但是,如果你把http放在前面,你会告诉链接它应该是一个绝对链接, 您的应用中可能有一个链接到test.pdf.old(作为示例)。然后你不会指望它转到http://test.pdf.old

WWW不是关键字,这意味着这是一个外部网页 - 它本质上是一种惯例。那么如果上面的pdf示例被称为www。您仍然希望下载它,而不是转到http://www.pdf.old

相关问题