【问题标题】:How to get the URL of the current page in C# [duplicate]如何在C#中获取当前页面的URL [重复]
【发布时间】:2010-10-10 06:32:04
【问题描述】:

谁能帮我在 C# 中获取 ASP.NET 当前工作页面的 URL?

【问题讨论】:

  • @Gordon 那是哪个?您将这个问题标记为重复的问题......您也标记为重复的问题,一分钟后。也许我们需要一个功能请求来让 StackOverflow 为标记的重复项添加“不关注”,因为我想要的只是一个答案,而搜索引擎首先把我带到了这里。但是,我不会提出这样的要求,以免它被标记为重复。 :(
  • @MarkAllen 不知道你为什么对我大喊大叫?如果您跟随骗子,您最终会遇到最古老的问题(我当时能找到)问这个问题。它是哪一个?嗯,他们所有人。你最终在哪里搜索并不重要。此页面为您提供 14 个答案,封闭的反对给您 15,原始的给您 11。如果我不关闭他们的问题,您只会得到 14,而不是 40。请注意,您的代表少于 10k所以你看不到已删除的。
  • @Gordon 对不起,如果帽子冒犯了你。如果可以在单个合并页面中暴露受骗者,那就太好了。
  • 在我看来,主要应该是问得最好(最高投票)或答案最好的那个。通常这将是最古老的,但并非总是如此。也许是元的转换...
  • 我同意 - 如果有办法将重复的问题合并为一个问题,那就太好了...

标签: c# asp.net


【解决方案1】:

您有时可能需要从 URL 获取不同的值。

以下示例显示了提取 URL 不同部分的不同方法

示例:(示例网址)

http://localhost:60527/WebSite1test/Default2.aspx?QueryString1=1&QueryString2=2

代码

Response.Write("<br/>Host " + HttpContext.Current.Request.Url.Host);
Response.Write("<br/>Authority: " + HttpContext.Current.Request.Url.Authority);
Response.Write("<br/>Port: " + HttpContext.Current.Request.Url.Port);
Response.Write("<br/>AbsolutePath: " + HttpContext.Current.Request.Url.AbsolutePath);
Response.Write("<br/>ApplicationPath: " + HttpContext.Current.Request.ApplicationPath);
Response.Write("<br/>AbsoluteUri: " + HttpContext.Current.Request.Url.AbsoluteUri);
Response.Write("<br/>PathAndQuery: " + HttpContext.Current.Request.Url.PathAndQuery);

输出

Host: localhost
Authority: localhost:60527
Port: 60527
AbsolutePath: /WebSite1test/Default2.aspx
ApplicationPath: /WebSite1test
AbsoluteUri: http://localhost:60527/WebSite1test/Default2.aspx?QueryString1=1&QueryString1=2
PathAndQuery: /WebSite1test/Default2.aspx?QueryString1=1&QueryString2=2

您可以复制粘贴上面的示例代码并在具有不同 URL 的 asp.net 网络表单应用程序中运行它。

我还建议阅读 ASP.Net Routing,以防您可能使用 ASP Routing,然后您就不需要使用带有查询字符串的传统 URL。

http://msdn.microsoft.com/en-us/library/cc668201%28v=vs.100%29.aspx

【讨论】:

  • 在前 2 个中,哪一个是安全方式?
  • 这肯定是最好的答案!
【解决方案2】:

request.rawurl 将给出当前页面的内容 它给出了您需要的确切路径

使用HttpContext.Current.Request.RawUrl

【讨论】:

    【解决方案3】:

    给需要 global.asax 文件中的路径/url 的人的提示;

    如果您需要在 global.asax > Application_Start 中运行它并且您的应用程序池模式是集成,那么您将收到以下错误:

    Request is not available in this context 异常 Application_Start。

    在这种情况下你需要使用这个:

    System.Web.HttpRuntime.AppDomainAppVirtualPath

    希望能帮助别人..

    【讨论】:

      【解决方案4】:

      如果你想得到

      localhost:2806 
      

      来自

      http://localhost:2806/Pages/ 
      

      然后使用:

      HttpContext.Current.Request.Url.Authority
      

      【讨论】:

        【解决方案5】:

        我想它足以返回绝对路径..

         Path.GetFileName( Request.Url.AbsolutePath )
        

        使用 System.IO;

        【讨论】:

        • 我认为这不会满足 OP,因为它只给出了页面名称:(
        【解决方案6】:

        感谢 Canavar 的帖子,这是我的解决方案。

        如果你有这样的事情:

        "http://localhost:1234/Default.aspx?un=asdf&somethingelse=fdsa"
        

        或者像这样:

        "https://www.something.com/index.html?a=123&b=4567"
        

        你只想要用户输入的部分然后这将起作用:

        String strPathAndQuery = HttpContext.Current.Request.Url.PathAndQuery;
        String strUrl = HttpContext.Current.Request.Url.AbsoluteUri.Replace(strPathAndQuery, "/");
        

        这将导致这些:

        "http://localhost:1234/"
        "https://www.something.com/"
        

        【讨论】:

        【解决方案7】:

        我在此页面上进行了一次搜索,但这并不是我想要的。在这里发帖,以防其他人也在此页面上寻找我的身份。

        如果你只有一个字符串值,有两种方法。

        .NET方式:

        与@Canavar 相同,但可以实例化一个新的 Uri 对象

        String URL = "http://localhost:1302/TESTERS/Default6.aspx";
        System.Uri uri = new System.Uri(URL);
        

        这意味着您可以使用相同的方法,例如

        string url = uri.AbsoluteUri;
        // http://localhost:1302/TESTERS/Default6.aspx
        
        string host = uri.host
        // localhost
        

        正则表达式方式:

        Getting parts of a URL (Regex)

        【讨论】:

        • 另外值得注意的是,如果无法解析字符串,则会抛出 System.FormatException。但是,System.Uri.TryCreate 方法是在 .NET 4 中引入的。
        【解决方案8】:

        试试这个:

        string url = HttpContext.Current.Request.Url.AbsoluteUri;
        // http://localhost:1302/TESTERS/Default6.aspx
        
        string path = HttpContext.Current.Request.Url.AbsolutePath;
        // /TESTERS/Default6.aspx
        
        string host = HttpContext.Current.Request.Url.Host;
        // localhost
        

        【讨论】:

        • AbsolutePath 似乎在 .NET 3.5 中返回“/”(尚未测试其他版本),用于类似于 twitter 用户帐户的路径,例如 twitter.com/#!/user。您可以使用 Fragment 方法获取井号 (#) 之后的任何内容。
        • 实际上,当与 UrlRewriter.net 之类的重写机制一起使用时,这些是不正确的,因为这将给出重写的 URL。然后您可以使用:Request.Url.GetLeftPart(UriPartial.Authority) + Request.RawUrl
        • 如果HttpContext.Current 为空怎么办?
        • @drzaus 如果HttpContext.Current 为空,那么您没有处理页面请求,或者您在设置之前尝试访问它。如果您需要更多帮助,请提出一个新问题。
        • 如果您需要在 global.asax > Application_Start 中运行它并且您的应用程序池模式是“集成的”,那么您将收到“Request is not available in this context exception in Application_Start”错误。在这种情况下,您需要使用 System.Web.HttpRuntime.AppDomainAppVirtualPath
        【解决方案9】:

        如果你只想要 http:// 和第一个斜杠之间的部分

        string url = Request.Url.Host;
        

        如果从此页面调用,将返回 stackoverflow.com

        这是complete breakdown

        【讨论】:

        • 不包括端口。需要:HttpContext.Current.Request.Url.Port.
        • @JohnB 或者更确切地说是HttpContext.Current.Request.Url.Authority,一键获取host:port...
        • 正如@JohnB 所说,如果端口不是 80 或 443,AuthorityHost 更安全。
        猜你喜欢
        • 2010-11-19
        • 2012-05-17
        • 2014-10-14
        • 2014-11-21
        • 2011-01-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-22
        相关资源
        最近更新 更多