Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Webmethod in aspx.cs page not able to access Page.Request object

Does anyone know how to access HttpRequest.Cookies within a static method with [WebMethod] attribute from an aspx.cs page?

It won't let me do it because method is static .

[WebMethod]
public static bool PostToTwitter(string identityUrl, string message, bool autoFollow)
{
  Page.Request.Cookies -- object reference is required for non-static field

Thanks!

like image 336
Sergey Avatar asked Sep 10 '09 01:09

Sergey


People also ask

Why is WebMethod static?

They're static because they are entirely stateless, they don't create an instance of your page's class and nothing is passed to them in the request (i.e. ViewState and form field values).

What is WebMethod C#?

The WebMethod attribute is added to each method we want to expose as a Web Service. ASP.NET makes it possible to map traditional methods to Web Service operations through the System. Web. Services. WebMethod attribute and it supports a number of properties that control the behavior of the methods.


1 Answers

Use,

HttpContext.Current.Request.Cookies
like image 153
KV Prajapati Avatar answered Oct 14 '22 21:10

KV Prajapati