kacalapy
kacalapy

Reputation: 10134

c# help using system.uri functions

i have a STRING in C# with a long url such as: http://mysite.com/testing/testingPages/area/ten/p1.aspx

how can i use system.uri class to get the http://mysite.com part only ?

Upvotes: 1

Views: 553

Answers (3)

casperOne
casperOne

Reputation: 74530

If you want to specifically get the part up to the domain (including scheme, username, password, and port), then you would call the GetLeftPart method on the Uri class like so:

Uri uri = new Uri("http://mysite.com/testing/testingPages/area/ten/p1.aspx");
string baseUri = uri.GetLeftPart(UriPartial.Authority);

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1499770

I believe Uri.GetLeftPart is what you're after:

using System;

public class Test
{
    static void Main()
    {
        string text = "http://mysite.com/testing/testingPages/area/ten/p1.aspx";
        Uri uri = new Uri(text);
        // Prints http://mysite.com
        Console.WriteLine(uri.GetLeftPart(UriPartial.Authority));
    }
}

Upvotes: 1

kacalapy
kacalapy

Reputation: 10134

Uri myURI = new Uri("http://mysite.com/testing/testingPages/area/ten/p1.aspx");

myURI.Host gets the domain or do whatever you want with the myURI object

Upvotes: 2

Related Questions