igorGIS
igorGIS

Reputation: 1956

Cannot access non-static 'Request' in static context

I've a web form (.aspx) and i want to initialize some fields with values from page request object.

 public partial class Freegitfs : System.Web.UI.Page
    {
        String _purchasebleUnitKey = Request["pu"] ?? String.Empty;
        ...

I get compiler warning 'Cannot access non-static 'Request' in static context' Why? my web form's class isn't static.

But if i reffer to HttpContext.Current.Request the warning is gone. Why so behavior?

Upvotes: 0

Views: 2286

Answers (1)

Ondrej Svejdar
Ondrej Svejdar

Reputation: 22074

Request is member property of Page class and you're trying it to access it before you have an instance of the class. The HttpContext.Current is static property, static property can be accessed without having instance (in your case class Freegitfs, which is inheriting from Page).

Also it is a good habit to distinguish between request types, so instead of Request["pu"] I would suggest Request.QueryString["pu"] or Request.Form["pu"]

Upvotes: 2

Related Questions