Get url without querystring

C#asp.net

C# Problem Overview


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.aspx from it.

Can you tell me how can I get it?

C# Solutions


Solution 1 - C#

Here's a simpler solution:

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

Borrowed from here: https://stackoverflow.com/questions/1188096/truncating-query-string-returning-clean-url-c-asp-net/1188180#1188180

Solution 2 - C#

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.

Solution 3 - C#

This is my solution:

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

Solution 4 - C#

Good answer also found here source of answer

Request.Url.GetLeftPart(UriPartial.Path)

Solution 5 - C#

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

Solution 6 - C#

My way:

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

or

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

Solution 7 - C#

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.

Solution 8 - C#

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:

Solution 9 - C#

System.Uri.GetComponents, just specified components you want.

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

Output:

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

Solution 10 - C#

Here's an extension method using @Kolman's answer. It's marginally easier to remember to use Path() than GetLeftPart. You might want to rename Path to GetPath, at least until they add extension properties to C#.

Usage:

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

The class:

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

Solution 11 - C#

Request.RawUrl.Split('?')[0]

Just for url name only !!

Solution 12 - C#

    string url = "http://www.example.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye";
    string path = url.split('?')[0];

Solution 13 - C#

Solution for Silverlight:

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

Solution 14 - C#

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() 

Solution 15 - C#

simple example would be using substring like :

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

Solution 16 - C#

var canonicallink = Request.Url.Scheme + "://" + Request.Url.Authority + Request.Url.AbsolutePath.ToString();

Solution 17 - C#

Try this:

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

from this: http://www.example.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye you'll get this: mypage.aspx

Solution 18 - C#

this.Request.RawUrl.Substring(0, this.Request.RawUrl.IndexOf('?'))

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRocky SinghView Question on Stackoverflow
Solution 1 - C#Johnny OshikaView Answer on Stackoverflow
Solution 2 - C#JoshView Answer on Stackoverflow
Solution 3 - C#KolmanView Answer on Stackoverflow
Solution 4 - C#Abdelfattah RagabView Answer on Stackoverflow
Solution 5 - C#tyyView Answer on Stackoverflow
Solution 6 - C#SatView Answer on Stackoverflow
Solution 7 - C#BrandonView Answer on Stackoverflow
Solution 8 - C#YogiView Answer on Stackoverflow
Solution 9 - C#RainVisionView Answer on Stackoverflow
Solution 10 - C#steviegView Answer on Stackoverflow
Solution 11 - C#Bhavishya Singh ChauhanView Answer on Stackoverflow
Solution 12 - C#Padam SinghView Answer on Stackoverflow
Solution 13 - C#IgorView Answer on Stackoverflow
Solution 14 - C#Sam JonesView Answer on Stackoverflow
Solution 15 - C#ParasView Answer on Stackoverflow
Solution 16 - C#Pradeep BalajiView Answer on Stackoverflow
Solution 17 - C#mirko cro 1234View Answer on Stackoverflow
Solution 18 - C#Prasad PatilView Answer on Stackoverflow