user131008
user131008

Reputation: 77

Request.ServerVariables in function

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: An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.Response.get'

Upvotes: 4

Views: 17868

Answers (1)

Tim M.
Tim M.

Reputation: 54377

Response is an instance property on the Page class, provided as a shortcut to HttpContext.Current.Response.

Either use an instance method, or use HttpContext.Current.Response.Write in your static method.

Examples

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

Or

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

The mention of a get() method in System.Web.UI.Page.Response.get refers to the property's get accessor. Essentially, it is saying that you can't call the get() method on an instance of a type from a static method of a type (which of course makes sense).

As a side note, Response.write(ref1); should be Response.Write() (corrected case).

Upvotes: 10

Related Questions