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

by APIJunkie 2/23/2008 10:22:00 AM

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

}

Currently rated 4.5 by 2 people

  • Currently 4.5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

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 us

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 us

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 us

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 us

2/25/2008 9:58:27 AM

APIJunkie

Thanks Nick, I think we have a winner Smile

Very nice solution!

James

APIJunkie us

Add comment


(Will show your Gravatar icon)  

  Country flag





Live preview

11/19/2008 6:52:48 PM

Powered by BlogEngine.NET 1.2.0.0
Theme by Mads Kristensen

About the author

Name of author

My name is Bacon…James Bacon.

I am an API wars veteran I was wounded by x86 assembly, recovered and moved on to C. I am currently stuck in C++ and sniffing .NET.

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.

E-mail me Send mail


Calendar

<<  November 2008  >>
MoTuWeThFrSaSu
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

View posts in large calendar

Recent comments

Authors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Sign in