【问题标题】:Get URL of current page in ASP.net [closed]在 ASP.net 中获取当前页面的 URL [关闭]
【发布时间】:2010-05-21 09:34:22
【问题描述】:

如何获取当前网页的完整url,包括http?例如:https://stackoverflow.com/

【问题讨论】:

  • 如何检索完整的网址?
  • 很高兴知道您正在考虑哪种输入。输入的是 IP 地址还是 URL 的一部分?否则这将无法回答。
  • 我投票赞成关闭,因为似乎没有人理解这个问题。
  • 你很幸运有人猜对了这个问题。提供更多有关您要完成的工作的信息真的太多工作了吗?

标签: asp.net url


【解决方案1】:

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

使用:Request.Url.ToString()

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

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

【讨论】:

    【解决方案2】:

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

    string completeUrl = Request.Url.ToString();
    

    【讨论】:

    • 请给我指定 Request.Url 的命名空间。谢谢
    • HttpRequest 类位于 System.Web 命名空间中。在我的 coe 示例中,RequestSystem.Web.Page 上的属性。 UserControlHttpContext 也有类似的属性。
    • 对不起..我没有使用用户控件...我在这里使用类文件..问题是我使用的是 dll..
    • 我不知道您要解决什么问题,我怀疑我的回答是否有用,但是 System.Web.HttpContext.Current.Request.Url.ToString() 呢?
    • 我要解决的是在 http 和 https 之间切换网站..
    【解决方案3】:

    已经有一段时间了,但是:

    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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-26
      • 2011-05-25
      • 2011-06-17
      • 2016-03-01
      • 2015-09-30
      • 1970-01-01
      • 2021-01-26
      相关资源
      最近更新 更多