C# SharePoint 在后面的代码中获取当前页面的完整 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1544268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:40:41  来源:igfitidea点击:

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

c#sharepoint

提问by raklos

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...

在 SharePoint 中,您如何从背后的代码中获取您所在页面的 url?例如,包含 blah.aspx 页面...

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

SPContext.Current.Web.Url 给出http://vm/en/

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

我需要它与http://vm/en/Pages/blah.aspx

采纳答案by Yuliy

You can still get the HttpContext and then use HttpContext.Current.Request.Url

您仍然可以获取 HttpContext 然后使用 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 是围绕您所在页面的 SPWeb,因此其 URL 是 Web 的 URL,而不是页面。

回答by Alex Angas

回答by Dinh Gia Khanh

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

尝试: SPContext.Current.Web.Url +"/"+ SPContext.Current.File.Url

回答by pns

This should return what you require SPContext.Current.ListItemServerRelativeUrl

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

回答by Mohamed.Abdo

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

回答by Mohamed.Abdo

string PageTitle=SPContext.Current.File.Title

回答by Sean

this code worked for me, for pages under _layouts and also for 'normal' pages under the site:

这段代码对我有用,适用于 _layouts 下的页面,也适用于网站下的“普通”页面:

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

回答by Oleg Savelyev

I use the workaround which covers _layouts cases

我使用涵盖 _layouts 案例的解决方法

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