Why is WebMethod declared static?

I declared WebMethod in default.aspx.cs ..

[WebMethod] public static void ResetDate() { LoadCallHistory(TheNewDate.Date); } 

Why should the WebMethod method be declared static?

+7
webmethod
source share
1 answer

They are static because they are completely stateless, they do not instantiate your page class, and nothing is passed to them in the request (i.e., ViewState and form field values).

HTTP is stateless by default, ASP.Net does a lot of things in the background using ViewState, Session, etc. during a standard page request to make life easier for developers.

When a web method is called through AJAX, the page does not send all the necessary form data that ASP.Net implements on the page to track the status of the request, because it leads to web methods too slowly; and if you need to process a lot, you have to move it to a dedicated web service.

You can access the methods on the page using HttpContext.CurrentHandler , which is explained in more detail here , as well as the current user, if you need it using HttpContext.Current.User .

There is an excellent article here explaining this in more detail.

+7
source share

All Articles