Reputation: 28555
In SharePoint how do you get the url of the page you are on from the code behind? e.g. with the blah.aspx page included...
SPContext.Current.Web.Url gives http://vm/en/
I need it with http://vm/en/Pages/blah.aspx
Upvotes: 23
Views: 99431
Reputation: 968
I use the workaround which covers _layouts cases
/// <summary>
/// Builds real URL considering layouts pages.
/// </summary>
private Uri CurrentUrl
{
get
{
return Request.Url.ToString().ToLower().Contains("_layouts")
? new Uri(
SPContext.Current.Site.WebApplication.GetResponseUri(
SPContext.Current.Site.Zone).ToString().TrimEnd('/')
+ Request.RawUrl)
: Request.Url;
}
}
Upvotes: 1
Reputation: 1064
this code worked for me, for pages under _layouts and also for 'normal' pages under the site:
string thisPageUrl;
if (HttpContext.Current.Request.Url.ToString().ToLower().Contains("_layouts"))
{
thisPageUrl = SPContext.Current.Web.Url + context.Request.Path; //note: cannot rely on Request.Url to be correct !
}
else
{
thisPageUrl = HttpContext.Current.Request.Url.ToString();
}
Upvotes: 1
Reputation: 17728
You can still get the HttpContext and then use HttpContext.Current.Request.Url
SPContext.Current.Web is the SPWeb surrounding the page you're on, and thus its URL is the URL of the Web, not the page.
Upvotes: 35
Reputation: 11
This should return what you require SPContext.Current.ListItemServerRelativeUrl
Upvotes: 1
Reputation: 31
Try : SPContext.Current.Web.Url +"/"+ SPContext.Current.File.Url
Upvotes: 3
Reputation: 60058
Try: SPContext.Current.File.Url
You could also use HttpContext.Current.Request.Url
Upvotes: 9