How to get a base URL from a given URL in ASP.NET

by APIJunkie 23. February 2008 10:22

Sometimes it is useful to get the base URL of a given URL.

For some reason there is no built in function in the URI class to do that.

A simple implementation of a function to get the base URL from a given URI would be:

////////////////////////////////////////////////////////////////

// Extract a base URL from a URL

////////////////////////////////////////////////////////////////

public Uri extractBaseURLFromURL(Uri url)

{

 
return new Uri(url.Scheme.ToString() + "://" + url.Authority);

}

////////////////////////////////////////////////////////////////

As a usage example if we call extractBaseURLFromURL and supply the following URL

http://msdn2.microsoft.com/en-us/netframework/default.aspx

The function will return the following URL 

http://msdn2.microsoft.com

----------

 Update:

----------

Nick Berardi offered a more elegant solution by using the GetLeftPart function (see discussion below):

Example:

// extract a base url from a url

public static Uri extractBaseURLFromURL(Uri url)

{

 
return new Uri(url.GetLeftPart(UriPartial.Authority));

}

Tags:

ASP.NET | How To | Web Development

Comments

2/23/2008 10:27:35 AM #

trackback

Trackback from DotNetKicks.com

How to get a base URL from a given URL in ASP.NET

DotNetKicks.com

2/24/2008 11:57:10 AM #

Nick Berardi

What is the matter with the built in function to do this?

Uri url = new Uri("msdn2.microsoft.com/.../default.aspx");
string basePart = url.GetLeftPart(UriPartial.Authority);

Nick Berardi United States

2/24/2008 11:58:44 AM #

Nick Berardi

That url didn't come out correctly at all.  But I guess you get the point.

Nick Berardi United States

2/25/2008 9:32:50 AM #

APIJunkie

Hi Nick,

Thank you for your comment.

Since it is such a useful thing, I believe a dedicated get base URL function that returns a URI should have been part of the URI class.

Declaration example:
public Uri getBase();

Usage example:
URI  url1 = url.getBase()

But I like your solution better since it is more elegant and will handle more general cases where the protocol is mailto: or news: etc..

See my comment on the original post ^

  James

APIJunkie United States

2/25/2008 9:46:10 AM #

Nick Berardi

If you are using .NET 3.5 try this.

namespace System {
    public static class Extensions {
        public static string GetBase (this Uri uri) {
            return uri.GetLeftPart(UriPartial.Authority);
        }
    }
}

It should satisfy your need as well as use the more elegant solution built into the .NET framework.

After this is included in your project you should be able to do the following:

Uri url = new Uri("msdn2.microsoft.com/.../default.aspx");
string baseUrl = url.GetBase();

That way you don't have to create a utility class to handle just the conversion.

Nick Berardi United States

2/25/2008 9:58:27 AM #

APIJunkie

Thanks Nick, I think we have a winner Smile

Very nice solution!

  James

APIJunkie United States

Comments are closed

About the author

Name of author

I was first wounded by x86 assembly, recovered and moved on to C. Following a long addiction to C++ and a short stint at rehab I decided to switch to a healthier addiction so I am now happily sniffing .NET and getting hooked on Silverlight.

I am mainly here to ramble about coding, various API’s, Junkies(me especially) and everything else that happens between coders and their significant other.

  James Bacon