【问题标题】:How to get URL path in C#如何在 C# 中获取 URL 路径
【发布时间】:2013-11-13 10:00:27
【问题描述】:

我想得到除了当前页面之外的所有URL路径,例如:我的URL是http://www.MyIpAddress.com/red/green/default.aspx 我只想得到“http://www.MyIpAddress.com/red/green/”。我怎么能得到。我正在做的像

string sPath = new Uri(HttpContext.Current.Request.Url.AbsoluteUri).OriginalString; System.Web.HttpContext.Current.Request.Url.AbsolutePath;
            sPath = sPath.Replace("http://", "");
            System.IO.FileInfo oInfo = new System.IO.FileInfo(sPath);
            string sRet = oInfo.Name;
            Response.Write(sPath.Replace(sRet, ""));

它在新 System.IO.FileInfo(sPath) 上显示异常,因为 sPath 包含“localhost/red/green/default.aspx”,表示“不支持给定路径的格式。”

【问题讨论】:

    标签: c# asp.net .net url


    【解决方案1】:

    使用这个

    string sPath = (HttpContext.Current.Request.Url).ToString();
    sPath = sPath.Replace("http://", "");
    var oInfo = new  System.IO.FileInfo(HttpContext.Current.Request.RawUrl);
    string sRet = oInfo.Name;
    Response.Write(sPath.Replace(sRet, ""));
    

    【讨论】:

      【解决方案2】:

      不要把它当作一个 URI 问题,把它当作一个字符串问题。然后就很简单了。

      String originalPath = new Uri(HttpContext.Current.Request.Url.AbsoluteUri).OriginalString;
      String parentDirectory = originalPath.Substring(0, originalPath.LastIndexOf("/"));
      

      真的就这么简单!

      已编辑以添加缺少的括号。

      【讨论】:

      • 这很危险,Shibu Thomas 的answer 更好。例如,考虑 URL 在查询字符串或片段中包含“/”的情况。
      • new Uri(...).OriginalString 与 ...AsboluteUri.ToString() 有什么区别?
      • 不满足所有 URIs...您假设字符串中会有 / 但有效的 URI 可能只有查询字符串接近尾声...
      【解决方案3】:

      主网址:http://localhost:8080/mysite/page.aspx?p1=1&p2=2

      在 C# 中获取 URL 的不同部分。

      Value of HttpContext.Current.Request.Url.Host
      localhost
      
      Value of HttpContext.Current.Request.Url.Authority
      localhost:8080
      
      Value of HttpContext.Current.Request.Url.AbsolutePath
      /mysite/page.aspx
      
      Value of HttpContext.Current.Request.ApplicationPath
      /mysite
      
      Value of HttpContext.Current.Request.Url.AbsoluteUri
      http://localhost:8080/mysite/page.aspx?p1=1&p2=2
      
      Value of HttpContext.Current.Request.RawUrl
      /mysite/page.aspx?p1=1&p2=2
      
      Value of HttpContext.Current.Request.Url.PathAndQuery
      /mysite/page.aspx?p1=1&p2=2
      

      【讨论】:

      • 如果您正在处理不在当前请求中的 Url,只需 new Uri(myUrlString); 然后利用这些属性。
      • 没有。这是一个误导的答案,因为它与问题的上下文无关,特别是与任何 URL 相关,而不仅仅是 ASP.NET Web 应用程序中可用的当前请求 URL。
      【解决方案4】:

      如果您只是想导航到您网站上的另一个页面,这可能会满足您的需求,但如果您真的需要它,它不会获得绝对路径。无需使用绝对路径即可在站点内导航。

      string loc = "";
      loc = HttpContext.Current.Request.ApplicationPath + "/NewDestinationPage.aspx";
      Response.Redirect(loc, true);
      

      如果你真的需要绝对路径,你可以挑选部分并使用 Uri 类构建你需要的东西:

      Uri myUri = new Uri(HttpContext.Current.Request.Url.AbsoluteUri)
      myUri.Scheme
      myUri.Host  // or DnsSafeHost
      myUri.Port
      myUri.GetLeftPart(UriPartial.Authority)  // etc.
      

      Good article 关于 ASP.NET 路径的主题。

      【讨论】:

        【解决方案5】:

        替换这个:

                    string sRet = oInfo.Name;
                    Response.Write(sPath.Replace(sRet, ""));
        

        以下:

                string sRet = oInfo.Name;           
                int lastindex = sRet.LastIndexOf("/");
                sRet=sRet.Substring(0,lastindex)
                Response.Write(sPath.Replace(sRet, ""));
        

        【讨论】:

          猜你喜欢
          • 2022-06-23
          • 2011-07-30
          • 2017-01-09
          • 2019-07-23
          • 1970-01-01
          • 2011-11-06
          • 1970-01-01
          • 1970-01-01
          • 2021-08-12
          相关资源
          最近更新 更多