SharePoint get the full URL of the current page in code behind

Viewed 97811

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

8 Answers

Try : SPContext.Current.Web.Url +"/"+ SPContext.Current.File.Url

This should return what you require SPContext.Current.ListItemServerRelativeUrl

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;
    }
}
Related