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;
}
}
Exactly what I was looking for, thanks!
ReplyDeleteStarting SharePoint 2013, you can use SPWeb.GetServerRelativeUrlFromUrl() method which accepts a full, server-relative or site-relative URL as a parameter and returns the server-relative URL.
ReplyDeleteThank you very much. Really helpful.
ReplyDeleteReally helpful and i got it fixed..
ReplyDeleteAlso can you help how to I get Site URL from the Absolute URL
ReplyDelete