【问题标题】:How get root url path如何获取根 url 路径
【发布时间】:2017-01-09 18:42:13
【问题描述】:

我正在寻找一种方法来获取我的 Web 项目的根 URL;例如:

本地:

http://localhost:52390/pages/user.aspx

预期结果:http://localhost:52390

IIS:

http://lst.pg.com/iLearn/pages/user.aspx

预期结果:http://lst.pg.com/iLearn

是否存在在 ASPX 中实现此目的的方法?还是在 Javascript/jQuery 中?

【问题讨论】:

    标签: javascript jquery asp.net url


    【解决方案1】:

    在 javascript 中,您可以从位置对象获取 url 信息。

    var href = location.href; //returns the entire url
    var host = location.hostname; //returns just the hostname of the url.
    

    【讨论】:

    • 带 href = http://localhost:52390/pages/user.aspx;与主机 = 本地主机
    • 正确。获得这些信息后,您可以轻松拆分字符串以获得所需的部分。
    【解决方案2】:

    ASPX:Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath

    用Javascript实现:

    <script type="text/javascript">
        var rootUrl = '<% =(Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath) %>';
    </script>
    

    【讨论】:

      【解决方案3】:

      有时我需要从后面的代码中获取根路径。这是我使用的。

      public static string GetRootPath()
      {
          HttpContext CTX;
          CTX = HttpContext.Current;
          return CTX.Request.ApplicationPath.ToString();
      }
      

      【讨论】:

        猜你喜欢
        • 2019-07-25
        • 2011-07-30
        • 2019-07-23
        • 2021-07-23
        • 2021-04-12
        • 2011-11-16
        • 1970-01-01
        • 2018-11-13
        • 2019-09-06
        相关资源
        最近更新 更多