【问题标题】:what is the best way to get the base URL from a controller从控制器获取基本 URL 的最佳方法是什么
【发布时间】:2011-12-26 20:39:09
【问题描述】:

在我的控制器中,我将如何获取基本 URL。

例如,如果我的网址是:

http://www.mysite.com/MyController/MyAction

我想要一个返回的函数:

http://www.mysite.com

【问题讨论】:

  • 你关心端口http://www.mysite.com:8000/MyController/MyAction吗?
  • 你是如何得到这个网址的?是请求网址吗?
  • @ErikPhilips 你有什么办法可以给http:// mysite.com : 8000
  • @Smith 你应该问一个新问题。

标签: asp.net-mvc


【解决方案1】:

我用:

Request.Url.GetLeftPart(UriPartial.Authority);

【讨论】:

    【解决方案2】:

    这是我在 c# 应用程序中使用的方法

     public static string base_url()
     {
      return string.Format("{0}://{1}/", HttpContext.Current.Request.Url.Scheme, HttpContext.Current.Request.Url.Authority);
     }
    

    请注意,如果您的开发服务器使用 80 以外的其他端口,这也会返回端口

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多