raklos
raklos

Reputation: 28555

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

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

Answers (8)

Oleg Savelyev
Oleg Savelyev

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

Sean
Sean

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

Mohamed.Abdo
Mohamed.Abdo

Reputation: 2200

string PageTitle=SPContext.Current.File.Title

Upvotes: -1

Mohamed.Abdo
Mohamed.Abdo

Reputation: 2200

 string filename = Path.GetFileName(Request.Path);

Upvotes: 0

Yuliy
Yuliy

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

pns
pns

Reputation: 11

This should return what you require SPContext.Current.ListItemServerRelativeUrl

Upvotes: 1

Dinh Gia Khanh
Dinh Gia Khanh

Reputation: 31

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

Upvotes: 3

Alex Angas
Alex Angas

Reputation: 60058

Try: SPContext.Current.File.Url

You could also use HttpContext.Current.Request.Url

Upvotes: 9

Related Questions