Thursday, November 15, 2007

Combining baseUri and RelativeUri - C#

Combining base Uri and relative Uri

        public string AddTwoUri(string baseUri, string relativeUri)
        {
            string retUri = "";
            if (!string.IsNullOrEmpty(baseUri))
            {
                if (!baseUri.EndsWith("/"))
                {
                    baseUri += "/";
                }
                Uri rootUri = new Uri(baseUri);
                if (!string.IsNullOrEmpty(relativeUri))
                {
                    relativeUri = relativeUri.Trim('/');
                    Uri davUri = new Uri(rootUri, relativeUri);
                    if (davUri != null)
                    {
                        retUri = davUri.ToString();
                    }
                }
                else
                {
                    throw new ArgumentNullException("relativeUri", "second parameter of AddTwoUri is null");
                }
            }
            else
            {
                throw new ArgumentNullException("baseUri", "first parameter of AddTwoUri is null");
            }
            return retUri;
        }

Sen Paravur

 

No comments: