0

I work on a ASP.NET MVC project, I create a Controller named AuthenticationController, this controller contains a static method called User_Access_Validation.

This method check some cookies values.

So that is method code :

public void User_Access_Validation()
{
 if (Request.Cookies["X_8"] == null || Request.Cookies["X_5"] == null || Request.Cookies["X_T"] == null)
   {
     Response.Redirect("/Login");
   }
}

Everythings works good at now, but when I change this method to a static method I got these four errors in ErrorList in Visual Studio

Error   CS0120  An object reference is required for the non-static field, method, or property 

Error   CS0120  An object reference is required for the non-static field, method, or property 

Error   CS0120  An object reference is required for the non-static field, method, or property

Error   CS0120  An object reference is required for the non-static field, method, or property 

So please I need this method to be a static method, and fix this error

ADS MB
  • 175
  • 11
  • Request is an instance property. Try HttpContext.Current.Request – Nikki9696 Jun 01 '20 at 21:19
  • I try it but `Current` not shown in Intellisense – ADS MB Jun 01 '20 at 21:23
  • Um. Where does this code live (a controller like you mentioned, an api method, some class somewhere else and Request isn't what I think it is) and what version of things are you using? https://learn.microsoft.com/en-us/dotnet/api/system.web.httpcontext.current?view=netframework-4.8 – Nikki9696 Jun 01 '20 at 21:30
  • yeah I Use controller but the `Current` don't shown in intellisense and **Visual Studio** show me an error **red underline** below it. – ADS MB Jun 01 '20 at 23:18
  • [This answer](https://stackoverflow.com/a/40029302/5101046) helps with providing static access to `HttpContext` (which includes request and response) in ASP.NET Core. This isn't easily available because it's not a good pattern. But, as noted in the answer, it might be helpful in cases where you need to move old code into a new controller. I'm curious about why it has to be static. But I get it. Sometimes we just need things to behave a certain way. – Scott Hannen Jun 02 '20 at 00:03

2 Answers2

1

Request and Response are instance properties - they belong to a specific instance of the Controller.

A static method doesn't belong to any specific instance of the class. From the documentation:

Static methods and properties cannot access non-static fields and events in their containing type, and they cannot access an instance variable of any object unless it is explicitly passed in a method parameter.

I recommend reading more. For the purposes of this answer, you can't access Request and Response from a static method. What you can do, as the article says, is pass them as arguments to a static method. So you could do this:

public static void User_Access_Validation(HttpRequest request, HttpResponse response)
{
 if (request.Cookies["X_8"] == null || request.Cookies["X_5"] == null || request.Cookies["X_T"] == null)
   {
     response.Redirect("/Login");
   }
}

Now the method isn't attempting to read those static properties. It's just using the methods passed to it. (Notice that request and response are lower-case. They're referring to the variables, not the properties which are capitalized.

You can call this static method from a non-static (instance) method. The instance method, which can access those properties, can pass them as arguments to the static method:

public static void User_Access_Validation(Request, Response);

Here's another good answer that explains the difference between static and instance (non-static) methods. It has a simple but illuminating example.

Scott Hannen
  • 27,588
  • 3
  • 45
  • 62
  • This answer is very literal. From the sound of it, unless it's for a quiz it might not help at all. Sorry. – Scott Hannen Jun 02 '20 at 00:04
  • This is a great answer and massive thanks bro for your time and your answer, I have one last question how can I create objects from `HttpRequest` and `HttpResponse` to pass them to the method, cause this is the first time I will work with them. – ADS MB Jun 02 '20 at 10:14
  • If you're calling from a non-static method then you don't have to create them. You can just pass the `Request` and `Response`. (See the last code sample at the end of the answer.) If you're calling from a static method then my answer doesn't help, because you're in the same place. – Scott Hannen Jun 02 '20 at 12:58
1

As Scott Hannen Montionned in his answer, you should use Response and Request objects as parameters if you need to work with static method, but I think your code should have like this :

public static void User_Access_Validation(HttpRequestBase request, HttpResponseBase response)
{
 if (request.Cookies["X_8"] == null || request.Cookies["X_5"] == null || request.Cookies["X_T"] == null)
   {
     response.Redirect("/Login");
   }
}

Whats new here ? is the parameters should base, HttpRequestBase and HttpResponseBase

I wish this help.

MBARK T3STO
  • 329
  • 1
  • 11