简体   繁体   中英

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? eg with the blah.aspx page included...

SPContext.Current.Web.Url gives http://vm/en/

I need it with http://vm/en/Pages/blah.aspx

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.

尝试:SPContext.Current.Web.Url +“/”+ SPContext.Current.File.Url

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();
        }

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

这应该返回您需要的SPContext.Current.ListItemServerRelativeUrl

 string filename = Path.GetFileName(Request.Path);
string PageTitle=SPContext.Current.File.Title

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM