2012-11-07 31 views
0

我有以下路径:如何操作c#asp.net中的URL路径?

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

,但我需要检索的第一个3个部分:

-http :// localhost/portal/

我试过HttpContext.Current.Request.Url.GetLeftPart();没有运气。

+3

如果放在Request.Url调试中断,你可以看到,可以给你的所有物品,像'.Path'和可以做的工作。比提出问题要快。 – Aristos

+0

这个问题可能对您有兴趣 http://stackoverflow.com/questions/4630249/get-url-without-querystring –

+0

总是前三部分或删除“reportview.aspx”?或者以不同的方式,如果url是'http:// localhost/portal/sub/page.aspx',怎么办? –

回答

1

试试这个;

string s = "http://localhost/portal/reportview.aspx"; 
string y = string.Format("{0}//{2}/{3}/",s.Split('/')); 
+0

Split在'//'之间找到一个空字符串,所以要么移动索引,要么在Split中使用StringSplitOptions.RemoveEmptyEntries。 –

+0

现在正在通过删除{1}来纠正。谢谢 – Kaf

1

您可以使用子字符串。

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

string left = str.Substring(0, str.LastIndexOf("/"));