How to get URL path in C#
Main URL : http://localhost:8080/mysite/page.aspx?p1=1&p2=2
Get different parts of URL in C#.
Value of HttpContext.Current.Request.Url.Host
localhost
Value of HttpContext.Current.Request.Url.Authority
localhost:8080
Value of HttpContext.Current.Request.Url.AbsolutePath
/mysite/page.aspx
Value of HttpContext.Current.Request.ApplicationPath
/mysite
Value of HttpContext.Current.Request.Url.AbsoluteUri
http://localhost:8080/mysite/page.aspx?p1=1&p2=2
Value of HttpContext.Current.Request.RawUrl
/mysite/page.aspx?p1=1&p2=2
Value of HttpContext.Current.Request.Url.PathAndQuery
/mysite/page.aspx?p1=1&p2=2
Don't treat it as a URI problem, treat it a string problem. Then it's nice and easy.
String originalPath = new Uri(HttpContext.Current.Request.Url.AbsoluteUri).OriginalString;
String parentDirectory = originalPath.Substring(0, originalPath.LastIndexOf("/"));
Really is that easy!
Edited to add missing parenthesis.
Replace this :
string sRet = oInfo.Name;
Response.Write(sPath.Replace(sRet, ""));
With following:
string sRet = oInfo.Name;
int lastindex = sRet.LastIndexOf("/");
sRet=sRet.Substring(0,lastindex)
Response.Write(sPath.Replace(sRet, ""));