Wednesday, March 16, 2011

C# Get RelativeUrl from AbsoluteUrl

Getting relativeUrl from absoluteUrl is very simple.

We have a class System.Uri which takes absolute/full url as a parameter and gives the relative url.
using System;
private static string GetRelativeUrl(string fullUrl)
{
   try
   {
      Uri uri = new Uri(fullUrl);//fullUrl is absoluteUrl
      string relativeUrl = uri.AbsolutePath;//The Uri property AbsolutePath gives the relativeUrl

      return relativeUrl;
   }
   catch (Exception ex)
   {
      return fullUrl;
      //throw ex;
   }
}

No comments:

Post a Comment

Post a Comment