2012-08-30 69 views
3
public static string Call() 
{ 
    string ref1 = HttpContext.Current.Request.ServerVariables["HTTP_REFERER"]; 
    Response.write(ref1); 
} 

public void Page_Load(object sender, EventArgs e) 
{ 
    Call() 
} 

CS0120:一個對象引用是所必需的非靜態字段, 方法,或屬性 'System.Web.UI.Page.Response.get'Request.ServerVariables在功能

回答

9

ResponsePage類中的一個實例屬性,作爲HttpContext.Current.Response的快捷方式提供。

請使用實例方法,或者在靜態方法中使用HttpContext.Current.Response.Write

例子

public static string Call() 
{ 
    string ref1 = HttpContext.Current.Request.ServerVariables["HTTP_REFERER"]; 
    HttpContext.Current.Response.Write(ref1); 
} 

或者

public string Call() 
{ 
    string ref1 = Request.ServerVariables["HTTP_REFERER"]; 
    Response.Write(ref1); 
} 

一個get()方法的System.Web.UI.Page.Response.get的提及指的是物業的get訪問。實際上,它是說你不能從一個類型的靜態方法(這當然是有道理的)對一個類型的實例調用get()方法。

作爲一個附註,Response.write(ref1);應該是Response.Write()(更正案例)。

相關問題