Wednesday, 13 February 2013

Getting the full url in the code behind of a SharePoint page in the layouts folder

The issue

I had a situation where I had created a sub site collection and a deployed a asmx websercice to a subfolder in the layouts folder.
From codebehind I used HttpContext.Current.Request.Url to get the url of the web service.

If I in the browser entered the address:
  • http://server/sites/subsitecol/_layouts/webservice.asmx
I got the url
  • http://server/_layouts/myfolder/webservice.asmx
in the codebehind. Notice it has omited the path to the sub site collection.

My workaround

The RawUrl contains the path to the sub site collection. However it is server relative, so I build the absolute url like this (there might be better ways to construct the absolute url):

var uri = new UriBuilder(HttpContext.Current.Request.Url);
uri.Path = HttpContext.Current.Request.RawUrl;
var absoluteurl = uri.Uri.AbsoluteUri;


No comments:

Post a Comment