rulururu

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

October 30th, 2008

Filed under: coding, programming — mike hall @ 1:41 am

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

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

ruldrurd

Powered by WordPress, Theme based off the "I'm Okay" theme by Laurentiu Piron

Creative Commons License This work is licensed under a Creative Commons Attribution 3.0 United States License.


Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.