231

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?

unor
  • 92,415
  • 26
  • 211
  • 360
Rocky Singh
  • 15,128
  • 29
  • 99
  • 146

19 Answers19

433

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: Truncating Query String & Returning Clean URL C# ASP.net

Community
  • 1
  • 1
Johnny Oshika
  • 54,741
  • 40
  • 181
  • 275
  • 24
    One line version: `return Request.Url.GetLeftPart(UriPartial.Path);` –  Sep 01 '16 at 19:10
  • 2
    `uri.GetComponent(` is another awesome method for getting parts of a Uri. I didn't know about these two until now! – AaronLS Oct 19 '17 at 21:06
141

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.

unor
  • 92,415
  • 26
  • 211
  • 360
Josh
  • 16,286
  • 25
  • 113
  • 158
  • 6
    url.AbsolutePath only returns the path portion of the URL (/mypage.aspx); prepend url.Scheme (http) + Uri.SchemeDelimiter (://) + url.Authority (www.somesite.com) for the full URL that you wanted – Ryan Jan 08 '11 at 02:09
  • 25
    Uri.GetLeftPart method is simpler as mentioned http://stackoverflow.com/questions/1188096/truncating-query-string-returning-clean-url-c-sharp-asp-net/1188180#1188180 – Edward Wilde Dec 19 '11 at 14:39
  • 1
    The `substring` method will give error if there's no Query string. Use `string path = url.Substring(0, url.IndexOf("?") > 0? url.IndexOf("?") : url.Length);` instead. – stomy Oct 18 '18 at 19:42
44

This is my solution:

Request.Url.AbsoluteUri.Replace(Request.Url.Query, String.Empty);
Egil
  • 5,600
  • 2
  • 32
  • 33
Kolman
  • 457
  • 4
  • 2
37

Good answer also found here source of answer

Request.Url.GetLeftPart(UriPartial.Path)
Nisarg Shah
  • 14,151
  • 6
  • 34
  • 55
36

Succinct solution:

Request.RawUrl.Split('?')[0];
Protector one
  • 6,926
  • 5
  • 62
  • 86
tyy
  • 361
  • 3
  • 2
16

My way:

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

or

new UriBuilder(url) { Query = string.Empty }.Uri
Sat
  • 1,240
  • 1
  • 10
  • 8
  • This is what i use for NET Core 1.0 project because it has not method `Uri.GetLeftPart`. Latest version of NET Core (1.1) should have that method (cant confirm because i'm not on net core 1.1 for now) – psulek Mar 23 '17 at 11:28
  • 1
    I like this one because building URIs is exactly what the UriBuilder is for. All the other answers are (good) hacks. – Russell Horwood Aug 01 '17 at 07:43
11

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.

Brandon
  • 68,708
  • 30
  • 194
  • 223
7

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:

Yogi
  • 6,241
  • 3
  • 24
  • 30
  • 2
    I'm rather shocked this hasn't gotten any upvotes until my own. This is a great solution. – jason Aug 28 '18 at 16:13
7

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
RainVision
  • 81
  • 1
  • 4
3

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);
        }
    }
}
stevieg
  • 652
  • 4
  • 14
3
Request.RawUrl.Split('?')[0]

Just for url name only !!

haldo
  • 14,512
  • 5
  • 46
  • 52
1
    string url = "http://www.example.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye";
    string path = url.split('?')[0];
0

Solution for Silverlight:

string path = HtmlPage.Document.DocumentUri.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped);
Igor
  • 3,576
  • 3
  • 23
  • 18
0

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() 
Sam Jones
  • 4,443
  • 2
  • 40
  • 45
0

.net core get url without querystring

var url = Request.GetDisplayUrl().Replace(Request.QueryString.Value.ToString(), "");
Vernard Sloggett
  • 336
  • 4
  • 10
-1

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("?"));
Paras
  • 240
  • 1
  • 13
-1
var canonicallink = Request.Url.Scheme + "://" + Request.Url.Authority + Request.Url.AbsolutePath.ToString();
CHEEKATLAPRADEEP
  • 12,191
  • 1
  • 19
  • 42
-2

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

-3
this.Request.RawUrl.Substring(0, this.Request.RawUrl.IndexOf('?'))
Adeel
  • 2,901
  • 7
  • 24
  • 34