【问题标题】:How to get BaseUrl from Context.Request in Asp.Net MVC?如何从 Asp.Net MVC 中的 Context.Request 获取 BaseUrl?
【发布时间】:2013-11-03 20:45:29
【问题描述】:

给定如下请求:

 http://www.sitename.com/controller/action/2

我正在尝试使用 Context.Request.Url 方法之一提取 http://www.sitename.com。我已经尝试过 AbsoluteUrl 和其他一些,但没有任何运气。是否有任何内置方法可以实现这一点,还是我必须编写自己的实用程序函数?

【问题讨论】:

    标签: asp.net .net asp.net-mvc asp.net-mvc-4


    【解决方案1】:
    HttpContext.Current.Request.Url.GetComponents(UriComponents.SchemeAndServer, 
                                                  UriFormat.SafeUnescaped)
    

    这将为您提供 http 或 https 以及来自给定 URL 的域。根据您使用此方法的位置,可能不需要 HttpContext.Current。

    【讨论】:

      【解决方案2】:

      您可以为此使用 URL 助手:

      Url.Content("~")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-10-14
        • 2011-11-16
        • 1970-01-01
        • 1970-01-01
        • 2019-01-24
        • 2018-01-11
        相关资源
        最近更新 更多