How to get the current URL from ASP.NET code behind
我的应用程序托管在不同的服务器上,我想获取当前服务器上页面的URL。
如何在代码隐藏中获取此属性?
1 | string url = HttpContext.Current.Request.Url.AbsoluteUri; |
http:/ / / / default.aspx thehost.com目录
1 | string path = HttpContext.Current.Request.Url.AbsolutePath; |
default.aspx /目录/
1 | string host = HttpContext.Current.Request.Url.Host; |
thehost.com
另一种方式获得从代码隐藏文件的URL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | public string FullyQualifiedApplicationPath { get { //Return variable declaration var appPath = string.Empty; //Getting the current context of HTTP request var context = HttpContext.Current; //Checking the current context content if (context != null) { //Formatting the fully qualified website url/name appPath = string.Format("{0}://{1}{2}{3}", context.Request.Url.Scheme, context.Request.Url.Host, context.Request.Url.Port == 80 ? string.Empty :":" + context.Request.Url.Port, context.Request.ApplicationPath); } if (!appPath.EndsWith("/")) appPath +="/"; return appPath; } } |
这个链接,你将得到更多的检查信息。
string path = HttpContext.Current.Request.Url.AbsolutePath;
1 | string MyUrl = HttpContext.Current.Request.Url.AbsoluteUri |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public string GetApplicationName(){ string url = HttpContext.Current.Request.Url.AbsoluteUri; int intStartIndex = GetIndex(url, 3); int intEndIndex = GetIndex(url, 4); return url.Substring(intStartIndex, (intEndIndex - intStartIndex) - 1); } public int GetIndex(string str, int indexNo){ int index = 0; for (int i = 0; i < indexNo; i++){ int tempIndex = str.IndexOf("/") + 1; str = str.Remove(0, tempIndex); index += tempIndex; } return index; } |