0

I have the following path:

-http://localhost/portal/reportview.aspx

but I need to retrieve the first 3 parts:

-http://localhost/portal/

Ive tried HttpContext.Current.Request.Url.GetLeftPart(); with no luck.

Farhad-Taran
  • 6,282
  • 15
  • 67
  • 121
  • 3
    If you place a debug break on the Request.Url you can see all the items that can give you , like the `.Path` and can do the job. Is faster than make a question. – Aristos Nov 07 '12 at 15:34
  • This question question might be interesting for you http://stackoverflow.com/questions/4630249/get-url-without-querystring – Illia Ratkevych Nov 07 '12 at 15:39
  • Always the first three parts or remove the "reportview.aspx"? Or to put it in a different way, what if the url is `http://localhost/portal/sub/page.aspx`? – Hans Kesting Nov 07 '12 at 15:41

2 Answers2

1

You can use substring.

string str = "http://localhost/portal/reportview.aspx";

string left = str.Substring(0, str.LastIndexOf("/"));
Adil
  • 146,340
  • 25
  • 209
  • 204
1

Try this;

string s = "http://localhost/portal/reportview.aspx";
string y = string.Format("{0}//{2}/{3}/",s.Split('/'));
Kaf
  • 33,101
  • 7
  • 58
  • 78
  • Split finds an empty string between the `//`, so either shift the indexes or use StringSplitOptions.RemoveEmptyEntries in the Split. – Hans Kesting Nov 07 '12 at 15:38