代码之家  ›  专栏  ›  技术社区  ›  Joel Coehoorn

在代码隐藏中获取ASP.NET页的URL[重复]

  •  182
  • Joel Coehoorn  · 技术社区  · 16 年前

    这个问题已经有了答案:

    我有一个ASP.NET页面,它将托管在几个不同的服务器上,我希望得到页面的URL(或者更好的是:页面所在的站点)作为字符串,以便在代码隐藏中使用。有什么想法吗?

    10 回复  |  直到 16 年前
        1
  •  211
  •   Mikey    16 年前

    Request.Url.AbsoluteUri

    http://..

        2
  •  116
  •   William    11 年前

    Request.Url.GetLeftPart(UriPartial.Authority)
    
        3
  •  27
  •   sth    14 年前

    Request.Url.GetLeftPart(UriPartial.Authority) +
            VirtualPathUtility.ToAbsolute("~/")
    
        4
  •  9
  •   Prescient    11 年前

    // get a sites base urll ex: example.com
    public static string BaseSiteUrl
    {
        get
        {
            HttpContext context = HttpContext.Current;
            string baseUrl = context.Request.Url.Authority + context.Request.ApplicationPath.TrimEnd('/');
            return baseUrl;
        }
    
    }
    

        6
  •  7
  •   Soner Gönül to StackOverflow    13 年前
    Request.Url.GetLeftPart(UriPartial.Authority) + Request.FilePath + "?theme=blue";
    

        7
  •  4
  •   Soner Gönül to StackOverflow    13 年前

    new Uri(Request.Url,Request.ApplicationPath)
    

    Request.Url.GetLeftPart(UriPartial.Authority)+Request.ApplicationPath
    
        8
  •  3
  •   Stephen Wrighton    16 年前

        9
  •  2
  •   Joel Coehoorn    13 年前

    Dim rawUrl As String = Request.RawUrl.ToString()
    
        10
  •  2
  •   von v.    12 年前

    <script type="text/javascript">
        alert('Server: ' + window.location.hostname);
        alert('Full path: ' + window.location.href);
        alert('Virtual path: ' + window.location.pathname);
        alert('HTTP path: ' + 
            window.location.href.replace(window.location.pathname, ''));    
    </script>