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

在SharePoint中,如何从后面的代码中获取您所在页面的URL? 例如,包括blah.aspx页面……

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

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

您仍然可以获取HttpContext,然后使用HttpContext.Current.Request.Url

SPContext.Current.Web是围绕您所在页面的SPWeb,因此其URL是Web的URL,而不是页面。

尝试: SPContext.Current.File 。 url

您也可以使用HttpContext.Current 。 Request.Url

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

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

此代码适用于我,适用于_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(); } 

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

 ///  /// Builds real URL considering layouts pages. ///  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; } } 
  string filename = Path.GetFileName(Request.Path); 
 string PageTitle=SPContext.Current.File.Title