Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Request.ServerVariables in function

Tags:

c#

asp.net

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'

like image 534
user131008 Avatar asked Dec 27 '22 18:12

user131008


1 Answers

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).

like image 158
Tim M. Avatar answered Dec 29 '22 10:12

Tim M.