我有一个这样的URL:

http://www.example.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye

我想从中得到http://www.example.com/mypage.aspx。

你能告诉我怎么买吗?


当前回答

试试这个:

urlString=Request.RawUrl.ToString.Substring(0, Request.RawUrl.ToString.IndexOf("?"))

从这里:http://www.example.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye 你会得到这个:mypage.aspx

其他回答

Request.RawUrl.Split(new[] {'?'})[0];

我的方法:

new UriBuilder(url) { Query = string.Empty }.ToString()

or

new UriBuilder(url) { Query = string.Empty }.Uri

Silverlight的解决方案:

string path = HtmlPage.Document.DocumentUri.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped);

简单的例子是使用子字符串:

string your_url = "http://www.example.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye";
string path_you_want = your_url .Substring(0, your_url .IndexOf("?"));

这里有一个更简单的解决方案:

var uri = new Uri("http://www.example.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye");
string path = uri.GetLeftPart(UriPartial.Path);

借用这里:截断查询字符串和返回干净的URL c# ASP.net