Get url without querystring

前端 未结 18 1992
后悔当初
后悔当初 2020-11-28 21:24

I have a URL like this:

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

I want to get http://www.example.com/mypage

相关标签:
18条回答
  • 2020-11-28 21:51

    I've created a simple extension, as a few of the other answers threw null exceptions if there wasn't a QueryString to start with:

    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;
    }
    

    Usage:

    var url = Request.Url?.AbsoluteUri.TrimQueryString() 
    
    0 讨论(0)
  • 2020-11-28 21:56

    This is my solution:

    Request.Url.AbsoluteUri.Replace(Request.Url.Query, String.Empty);
    
    0 讨论(0)
  • 2020-11-28 21:56

    Split() Variation

    I just want to add this variation for reference. Urls are often strings and so it's simpler to use the Split() method than Uri.GetLeftPart(). And Split() can also be made to work with relative, empty, and null values whereas Uri throws an exception. Additionally, Urls may also contain a hash such as /report.pdf#page=10 (which opens the pdf at a specific page).

    The following method deals with all of these types of Urls:

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

    Example Output:

    • null ---> empty
    • empty ---> empty
    • http://domain/page.html ---> http://domain/page.html
    • http://domain/page.html?q=100 ---> http://domain/page.html
    • http://domain/page.html?q=100#page=2 ---> http://domain/page.html
    • http://domain/page.html#page=2 ---> http://domain/page.html

    • page.html ---> page.html

    • page.html?q=100 ---> page.html
    • page.html?q=100#page=2 ---> page.html
    • page.html#hash ---> page.html
    0 讨论(0)
  • 2020-11-28 21:56

    Solution for Silverlight:

    string path = HtmlPage.Document.DocumentUri.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped);
    
    0 讨论(0)
  • 2020-11-28 21:58

    You can use Request.Url.AbsolutePath to get the page name, and Request.Url.Authority for the host name and port. I don't believe there is a built in property to give you exactly what you want, but you can combine them yourself.

    0 讨论(0)
  • 2020-11-28 21:59

    You can use 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);
    

    Or you can use substring

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

    EDIT: Modifying the first solution to reflect brillyfresh's suggestion in the comments.

    0 讨论(0)
提交回复
热议问题