在SharePoint中,您如何從代碼背後獲取您所在頁面的網址? 例如與包括blah.aspx網頁...SharePoint在代碼背後的代碼中獲取當前頁面的完整URL
SPContext.Current.Web.Url給http://vm/en/
我需要它http://vm/en/Pages/blah.aspx
在SharePoint中,您如何從代碼背後獲取您所在頁面的網址? 例如與包括blah.aspx網頁...SharePoint在代碼背後的代碼中獲取當前頁面的完整URL
SPContext.Current.Web.Url給http://vm/en/
我需要它http://vm/en/Pages/blah.aspx
你仍然可以使用的HttpContext,然後使用HttpContext.Current.Request.Url
SPContext.Current.Web是圍繞着你正在瀏覽的網頁中的SPWeb,因而其URL是網站的URL,而不是頁面。
嘗試:SPContext.Current.Web.Url + 「/」 + SPContext.Current.File.Url
這應返回您所需要的SPContext.Current.ListItemServerRelativeUrl
string filename = Path.GetFileName(Request.Path);
string PageTitle=SPContext.Current.File.Title
此代碼爲我工作,對於_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();
}
這將排除可能的查詢字符串。 – 2013-11-26 10:09:08
我使用覆蓋_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;
}
}
只是要清楚,使用完整的參數是System.Web.HttpContext.Current.Request.Url – 2011-04-20 14:33:50
嗯,是的,這個類的HttpContext是System.Web命名空間 – Yuliy 2011-04-21 19:06:57
剛如果你正在使用ManagedPaths和/ _layouts /應用程序頁面做這件事,請小心。 ex./sites/1/_layouts/page.aspx將是/_layouts/page.aspx – HaavardMeling 2012-03-22 15:43:07