在SharePoint中,如何从后台代码获取当前页面的URL?
例如,包括blah.aspx页面...
SPContext.Current.Web.Url给出了http://vm/en/。
我需要的是http://vm/en/Pages/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,而不是页面的URL。
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();
}
这应该返回您需要的内容:SPContext.Current.ListItemServerRelativeUrl
/// <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;
}
}
string filename = Path.GetFileName(Request.Path);
string PageTitle=SPContext.Current.File.Title