How to get original Request URL in SharePoint

If you need to get current request URL in ordinary web application, you usually do

string requestURL = Request.Url.AbsoluteUri;

But as SharePoint does it’s own URL rewrites, if you would try that for example for the page

http://yourdomain/sites/yoursite/_layouts/YourPage.aspx

It would return

http://yourdomain/_layouts/YourPage.aspx

The solution is to use special property on SPUtility class – SPUtility.OriginalServerRelativeRequestUrl

This property returns the original URL before it was rewritten, which is useful if you need to get the subweb from which an application page was loaded.

As this URL is server-relative, to get full url, you can use:

SPUtility.GetFullUrl(SPContext.Current.Site, SPUtility.OriginalServerRelativeRequestUrl);

This can be useful if you need to redirect for example to the same URL with some additional parameters.

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *