Dum*_*tac 2 c# asp.net iis url
我创建了几个虚拟导演,我希望能够从当前的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);
Run Code Online (Sandbox Code Playgroud)
我试过
string urlApp = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + "/";
Run Code Online (Sandbox Code Playgroud)
在localhost中工作得很好:http://localhost:2468/test.aspx结果http://localhost:2468/
,但是当通过虚拟目录访问http://myhost/app_1/test.aspx结果时http://myhost/
我怎么能得到http://myhost/app_1/?
HttpContext.Current.Request.ApplicationPath
Run Code Online (Sandbox Code Playgroud)
是您获取到虚拟目录的路由所需的.将其附加到您已经获得的网址上,然后您就会成为赢家.就像是
HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + HttpContext.Current.Request.ApplicationPath
Run Code Online (Sandbox Code Playgroud)
应该工作,但要注意拖尾斜线.
| 归档时间: |
|
| 查看次数: |
1462 次 |
| 最近记录: |