获取ASP.net中当前页面的URL

时间:2010-05-21 09:34:22

标签: asp.net url

如何检索当前网页的完整网址,包括http?例如:https://stackoverflow.com/

3 个答案:

答案 0 :(得分:5)

也许你的意思是获取当前页面的网址?

使用:Request.Url.ToString()

或者如果您想将相对Url转换为绝对路径,我相信代码是这样的:

Request.Url.Host + Page.ResolveUrl(relativeUrl)

答案 1 :(得分:3)

如果您要查找当前请求上下文中的完整URL,HttpRequest.Url属性应该可以解决问题。要在string中获得Page代表:

string completeUrl = Request.Url.ToString();

答案 2 :(得分:2)

已经有一段时间但是:

Request.ServerVariables["Url"];

http://msdn.microsoft.com/en-us/library/ms525396(VS.90).aspx

甚至是1998年的参考文献!

http://www.4guysfromrolla.com/webtech/092298-3.shtml

修改

来自http://www.w3schools.com/asp/coll_servervariables.asp

<html>
<body>
<p>
<b>You are browsing this site with:</b>
<%Response.Write(Request.ServerVariables("http_user_agent"))%>
</p>
<p>
<b>Your IP address is:</b>
<%Response.Write(Request.ServerVariables("remote_addr"))%>
</p>
<p>
<b>The DNS lookup of the IP address is:</b>
<%Response.Write(Request.ServerVariables("remote_host"))%>
</p>
<p>
<b>The method used to call the page:</b>
<%Response.Write(Request.ServerVariables("request_method"))%>
</p>
<p>
<b>The server's domain name:</b>
<%Response.Write(Request.ServerVariables("server_name"))%>
</p>
<p>
<b>The server's port:</b>
<%Response.Write(Request.ServerVariables("server_port"))%>
</p>
<p>
<b>The server's software:</b>
<%Response.Write(Request.ServerVariables("server_software"))%>
</p>
</body>
</html>