Add something new to Virb:

Virb

Are you sure you want to delete that?

or Cancel

 

Posted on Oct 30, 2008

I Have a Path.Combine, But How About a Url.Combine?

If you're wanting to combine two URLs with Path.Combine, then you're likely to wind up with something like "http://bitterware.com\download.html"... and that's not very good. What we need is something like a Url.Combine. Well, we have something like that. We have Uri.TryCreate, but it's not as pretty as Path.Combine. So I prettied it up, wrapped it up and thought I'd share:


namespace Bitter
{
    namespace Path
    {
        public static class Url
        {
            public static string Combine(string domain, string page)
            {
                string combinedUrl = String.Empty;
                Uri baseUri = null;

                // create the URI object from the string domain
                try
                {
                    baseUri = new Uri(domain);
                }
                catch (Exception ex)
                {
                    baseUri = null;
                }

                // try to combine and create the new URI
                if (baseUri != null)
                {
                    Uri value = null;
                    if (Uri.TryCreate(baseUri, page, out value))
                        combinedUrl = value.ToString();
                }

                return combinedUrl;
            }

...





Loading comments...

Likes

Details

Viewed 4 times

© 2008 mike6024

virb.com/t/901164
tweet!

Flag this text post!

Flag this text post as:

or Cancel

 

Advertisement

Flag this profile!

Flag this profile as:

or Cancel