【问题标题】:Absolute URLs not working with Virtual Directories?绝对 URL 不适用于虚拟目录?
【发布时间】:2012-05-29 00:40:49
【问题描述】:

我创建了几个虚拟导向器,我希望能够从当前的 http 请求中获取 url。

例如:

http://www.site.com/app_1/default.aspx ===> http://www.site.com/app_1/

http://www.site.com/app_2/default.aspx ===> http://www.site.com/app_2/

....

http://www.site.com/app_n/default.aspx ===> http://www.site.com/app_n/

我的代码:

    string urlApp = HttpContext.Current.Request.Url.AbsoluteUri.ToString();
    urlApp = urlApp.Substring(0, urlApp.LastIndexOf('/') + 1);

我试过了

    string urlApp = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + "/";

在 localhost 中效果很好:http://localhost:2468/test.aspx 结果http://localhost:2468/

,但是通过虚拟目录访问时http://myhost/app_1/test.aspx结果http://myhost/

我怎样才能得到http://myhost/app_1/

【问题讨论】:

  • 您是否尝试过查看HttpContext.Current.Request.Url.AbsoluteUri.ToString(); 实际返回的内容?也许它只是返回http://myhost/app_1/,然后您的子字符串正在删除 app_1 部分?

标签: c# asp.net url iis


【解决方案1】:
HttpContext.Current.Request.ApplicationPath 

是获取到虚拟目录的路由所需要的。将其附加到您已经获得的 URL 上,您就赢了。类似的东西

HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + HttpContext.Current.Request.ApplicationPath

应该可以,但要注意尾部的斜杠。

【讨论】:

  • @dumitru 请也给答案投票
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-29
  • 2013-01-13
  • 1970-01-01
  • 1970-01-01
  • 2016-06-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多