为什么的WebMethod声明为static? [英] Why WebMethod declared as Static?

查看:1348
本文介绍了为什么的WebMethod声明为static?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我宣布我default.aspx.cs文件一个WebMethod ..

I declared a WebMethod in my default.aspx.cs file..

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

为什么一定要WebMethod的方法声明为static?

Why must the WebMethod method be declared static?

推荐答案

他们是静态的,因为它们是完全无状态的,他们不创造你的页面的类并没有什么传递给他们的要求的一个实例(即ViewState中和表单字段值)。

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

HTTP是无状态的默认情况下,ASP.Net做了很多的东西,在一个标准的页面请求期间的ViewState,会话等背景,使生活更轻松的开发

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

当一个Web方法是通过AJAX调用时,页面不发送所有必要的表单数据ASP.Net嵌入在页面中跟踪请求的状态,因为它将使Web方法太慢;如果你需要做大量的处理你应该把它搬出去一个专门的网络服务,而不是

When a web method is called through AJAX, the page isn't sending all the necessary form data ASP.Net embeds in a page to keep track of request state because it would make web methods too slow; and if you need to do a lot of processing you should move it out to a dedicated web service instead.

您可以访问使用的 HttpContext.CurrentHandler 的哪个更详细<一个解释href=\"http://stackoverflow.com/questions/2495194/access-current-instance-of-page-from-a-static-class\">here而且,如果你通过的 HttpContext.Current.User

You can get access to methods on the page using HttpContext.CurrentHandler which is explained in more detail here and also the current user if you need it via HttpContext.Current.User.

有一个很好的文章一>更详细地解释这一点。

There's an excellent article here explaining this in more detail.

这篇关于为什么的WebMethod声明为static?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆