获取没有查询字符串的URL


189

我有这样的网址:

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

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

你能告诉我如何获得吗?

Answers:


129

您可以使用 System.Uri

Uri url = new Uri("http://www.example.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye");
string path = String.Format("{0}{1}{2}{3}", url.Scheme, 
    Uri.SchemeDelimiter, url.Authority, url.AbsolutePath);

或者你可以使用 substring

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

编辑:修改第一个解决方案以在评论中反映brillyfresh的建议。


6
url.AbsolutePath仅返回URL(/mypage.aspx)的路径部分;在URL.Scheme(http)+ Uri.SchemeDelimiter(://)+ url.Authority(www.somesite.com)之前添加所需的完整URL
Ryan

19
Uri.GetLeftPart方法是上述简单stackoverflow.com/questions/1188096/...
爱德华·王尔德

substring如果没有查询字符串,该方法将给出错误。使用string path = url.Substring(0, url.IndexOf("?") > 0? url.IndexOf("?") : url.Length);代替。
造口

378

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

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

从这里借来的:截断查询字符串并返回干净的URL C#ASP.net


17
一线版本:return Request.Url.GetLeftPart(UriPartial.Path);
jp2code

uri.GetComponent(是获取Uri零件的另一种很棒的方法。直到现在我都不知道这两个!
AaronLS

38

这是我的解决方案:

Request.Url.AbsoluteUri.Replace(Request.Url.Query, String.Empty);

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

2
我喜欢这个,只是因为您无需使用完整的uri就可以使用它。
KingOfHypocrites

请记住,这RawUrl URL重写之前获得URL,所以也许使用AbsoluteUriRequest.Url.AbsoluteUri.Split('?')[0]
保护者


16

我的方式:

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

要么

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

这就是我用于NET Core 1.0项目的方法,因为它没有方法Uri.GetLeftPart。NET Core(1.1)的最新版本应具有该方法(无法确认,因为我暂时不在Net Core 1.1上)
psulek

1
我喜欢这一点,因为构建URI正是UriBuilder的目的。所有其他答案都是(好的)hack。
九尾巴

11

您可以使用Request.Url.AbsolutePath获取页面名称以及Request.Url.Authority主机名和端口。我不相信有内置属性可以为您提供所需的确切信息,但您可以自己组合它们。


1
它给了我/mypage.aspx,而不是我想要的。
Rocky Singh

4

Split()变化

我只想添加此变体以供参考。Urls通常是字符串,因此使用Split()方法比更为简单Uri.GetLeftPart()。并且Split()还可以使其与相对,空和null值一起工作,而Uri引发异常。此外,Ulls还可能包含一个散列,例如/report.pdf#page=10(在特定页面上打开pdf)。

以下方法处理所有这些类型的Urls:

   var path = (url ?? "").Split('?', '#')[0];

示例输出:


1
我很震惊,直到我自己没有得到任何投票。这是一个很好的解决方案。
杰森

3

这是使用@Kolman答案的扩展方法。与GetLeftPart相比,记住使用Path()稍微容易一点。您可能想要将Path重命名为GetPath,至少直到它们将扩展属性添加到C#为止。

用法:

Uri uri = new Uri("http://www.somewhere.com?param1=foo&param2=bar");
string path = uri.Path();

班上:

using System;

namespace YourProject.Extensions
{
    public static class UriExtensions
    {
        public static string Path(this Uri uri)
        {
            if (uri == null)
            {
                throw new ArgumentNullException("uri");
            }
            return uri.GetLeftPart(UriPartial.Path);
        }
    }
}



0

Silverlight解决方案:

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

0

我创建了一个简单的扩展,因为其他一些答案如果没有以a QueryString开头的话会抛出null异常:

public static string TrimQueryString(this string source)
{ 
    if (string.IsNullOrEmpty(source))
            return source;

    var hasQueryString = source.IndexOf('?') != -1;

    if (!hasQueryString)
        return source;

    var result = source.Substring(0, source.IndexOf('?'));

    return result;
}

用法:

var url = Request.Url?.AbsoluteUri.TrimQueryString() 

0

System.Uri.GetComponents,只是您想要的指定组件。

Uri uri = new Uri("http://www.example.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye");
uri.GetComponents(UriComponents.SchemeAndServer | UriComponents.Path, UriFormat.UriEscaped);

输出:

http://www.example.com/mypage.aspx

-1

一个简单的示例将使用子字符串,如:

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


-2

试试这个:

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

从此:http ://www.example.com/mypage.aspx?myvalue1=hello&myvalue2=再见,您将获得:mypage.aspx


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.