2010-01-12 52 views
1

我建設我VS2008 .NET 3.5溶液時收到錯誤 錯誤1是所必需的非靜態字段,方法或屬性的對象引用「System.Web.UI.Page.Request.get」一個對象引用是所必需的非靜態字段,方法或屬性

String _XSLTPath = Page.Request.Url.Scheme 
    + "://" 
    + Page.Request.Url.Authority 
    + Page.Request.ApplicationPath.TrimEnd('/') 
    + '/' 
    + "webparts/weatherandtime/weather/xslt/RSSWeatherXSL.xsl"; 

Page對象似乎在綠色被higlighting這不是我想要的。有人可以解釋發生了什麼事嗎?

感謝,

+0

額外的括號已被刪除,但還是引起了錯誤。看起來很奇怪,我可以在代碼中得到一個對象的實例,只是在代碼中有幾行而沒有實例化之前? – test 2010-01-12 19:22:47

+0

你能用這個資格頁嗎? this.Page.Request ...? – 2010-01-12 20:29:17

回答

1

您嘗試訪問非靜態屬性Page.Request沒有一個實例。你必須在實例上調用它。類似於myPage.Request

+0

@Daniel這看起來像是一個存在於控件中的代碼。控制有this.Page財產......所以這段代碼應該是有效的。我認爲他的問題是在聲明結尾的附加括號。 – 2010-01-12 19:15:43

0

你可能要考慮使用StringBuilder的,使這個多一點易於管理:

using System.Text; 

StringBuilder sb = new StringBuilder(); 

// if this is a control or WebPart, replace Request with this.Page.Request 
sb.Append(Request.Url.Scheme); 
sb.Append("://"); 
sb.Append(Request.Url.Authority); 
sb.Append(Request.ApplicationPath.TrimEnd('/'); 
sb.Append("/"); 
sb.Append("webparts/weatherandtime/weather/xslt/RSSWeatherXSL.xsl"); 

String _XSLTPath = sb.ToString(); 
0

嘗試使用Page.Context代替:

String _XSLTPath2 = Context.Request.Url.Scheme 
        + "://" 
        + Context.Request.Url.Authority 
        + Context.Request.ApplicationPath.TrimEnd('/') 
        + '/' 
        + "webparts/weatherandtime/weather/xslt/RSSWeatherXSL.xsl"; 
0

你們是不是要使用頁面屬性控制從一個方法或屬性定義爲static

很難清楚地看到發生了什麼,沒有看到代碼的完整的上下文,但可以解釋爲什麼你在代碼中的一個部分看到的問題,但不是在另一個。

0

通常這是一個正確的做法:

String _XSLTPath = HttpContext.Current.Request.Url.Scheme; 
相關問題