【发布时间】: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 部分?