2009-10-09 92 views

回答

33

你仍然可以使用的HttpContext,然後使用HttpContext.Current.Request.Url

SPContext.Current.Web是圍繞着你正在瀏覽的網頁中的SPWeb,因而其URL是網站的URL,而不是頁面。

+7

只是要清楚,使用完整的參數是System.Web.HttpContext.Current.Request.Url – 2011-04-20 14:33:50

+1

嗯,是的,這個類的HttpContext是System.Web命名空間 – Yuliy 2011-04-21 19:06:57

+5

剛如果你正在使用ManagedPaths和/ _layouts /應用程序頁面做這件事,請小心。 ex./sites/1/_layouts/page.aspx將是/_layouts/page.aspx – HaavardMeling 2012-03-22 15:43:07

3

嘗試:SPContext.Current.Web.Url + 「/」 + SPContext.Current.File.Url

1

這應返回您所需要的SPContext.Current.ListItemServerRelativeUrl

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

此代碼爲我工作,對於_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(); 
     } 
+0

這將排除可能的查詢字符串。 – 2013-11-26 10:09:08

1

我使用覆蓋_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; 
    } 
} 
相關問題