27

I want to get the all the path of URL except the current page of url, eg: my URL is http://www.MyIpAddress.com/red/green/default.aspx I want to get "http://www.MyIpAddress.com/red/green/" only. How can I get.I'm doing like

string sPath = new Uri(HttpContext.Current.Request.Url.AbsoluteUri).OriginalString; System.Web.HttpContext.Current.Request.Url.AbsolutePath;
            sPath = sPath.Replace("http://", "");
            System.IO.FileInfo oInfo = new System.IO.FileInfo(sPath);
            string sRet = oInfo.Name;
            Response.Write(sPath.Replace(sRet, ""));

Its showing exception on new System.IO.FileInfo(sPath) as sPath contain "localhost/red/green/default.aspx" saying "The given path's format is not supported."

0

5 Answers 5

105

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
Sign up to request clarification or add additional context in comments.

2 Comments

And if you are dealing with a Url not in the current request, just new Uri(myUrlString); then leverage these properties.
No. This is a misleading answer, as it doesn't relate to the context of the question, specifically relating to any URL, not just the current Request Url available in an ASP.NET web application.
12

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.

3 Comments

This is dangerous, Shibu Thomas' answer is better. Consider for example the situation when the URL would contain a '/' in the query string or fragment.
What's the difference between new Uri(...).OriginalString vs ...AsboluteUri.ToString() ?
Does not cater for all URIs... you are assuming there will be a / in the string but a valid URI could just have querystrings towards the end...
3

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, ""));

Comments

2

use this

string sPath = (HttpContext.Current.Request.Url).ToString();
sPath = sPath.Replace("http://", "");
var oInfo = new  System.IO.FileInfo(HttpContext.Current.Request.RawUrl);
string sRet = oInfo.Name;
Response.Write(sPath.Replace(sRet, ""));

Comments

0

This may get you want you want if you're just trying to navigate to another page on your site, but it doesn't get the absolute path if you really need that. You can navigate within the site without using the absolute path.

string loc = "";
loc = HttpContext.Current.Request.ApplicationPath + "/NewDestinationPage.aspx";
Response.Redirect(loc, true);

If you really need the absolute path, you can pick off the parts and build what you need with the Uri class:

Uri myUri = new Uri(HttpContext.Current.Request.Url.AbsoluteUri)
myUri.Scheme
myUri.Host  // or DnsSafeHost
myUri.Port
myUri.GetLeftPart(UriPartial.Authority)  // etc.

Good article on the subject of ASP.NET paths.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.