如何从Web方法调用普通方法? [英] How to call a normal method from a webmethod?

查看:87
本文介绍了如何从Web方法调用普通方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我需要创建一个webmethod和一个普通方法,并需要从该webmethod调用该普通方法.

我只需要一个小示例来说明如何执行此任务.任何人都可以帮助我做到这一点.

Hi All,

i need to create a webmethod and a normal method and need to call the normal method from the webmethod.

I just need a small Example for how to do this task.Can anyone help me in doing this..??

推荐答案

我同意@aspnet_regiis.

此处 [
I agree with @aspnet_regiis.

The same type of question is discussed here[^].

The WebMethods are always atatic.
For details check the link below.
为什么ASP.NET AJAX页面方法必须是静态的? [
那么,为什么页面方法调用必须是静态的?

如果您要实现页面方法,则您可能很清楚它们的出色性能.与UpdatePanel的部分回发相比,它们的性能尤其出色.

它们之所以有效,主要是因为它们不需要POST ViewState,也不需要创建Page的实例,而部分回发则可以同时完成这两项.众所周知,由于请求中未提供ViewState,因此即使有需要,page方法也无法创建该页面的实例.

这就是为什么必须将它们标记为静态的原因.它们无法与Page类的实例属性和方法进行交互,因为page方法调用不会创建Page实例或其任何控件.

页面方法大致等同于独立Web服务的简写形式.实际上,ScriptManager甚至完全像常规Web服务一样调用它们.


So, why do page method calls have to be static?

If you’re implementing page methods, you’re probably well aware of their excellent performance. They are especially performant compared to the UpdatePanel’s partial postbacks.

They are efficient largely because they do not require the ViewState to be POSTed and they do not create an instance of the Page, while partial postbacks do both of these things. As we now know, a page method couldn’t create an instance of the page even if desired, since the ViewState isn’t provided in the request.

This is exactly why they must be marked as static. They cannot interact with the instance properties and methods of your Page class, because a page method call creates no instance of the Page or any of its controls.

Page methods are roughly equivalent to shorthand for standalone web services. In fact, the ScriptManager even calls them exactly as it would a regular web service.

浏览链接并学习基础知识.

由于WebMethod是静态的,因此无法调用Page类的一种常规方法.
如果要在WebMethod中调用一个方法,则需要将其标记为Static.

谢谢...

Go through the link and learn the fundamentals.

As the WebMethod is static, it cannot call one normal method of the Page class.
If you want to call one method inside the WebMethod, you need to mark it as Static.

Thanks...


您将要调用的私有方法声明为静态
You declare the private method what you wants to call as static
[WebMethod]
public static string MyWebMethod(string postCode)
{
// My webmethod code
string str = MyPrivateMethod(2,5);
}

private static string MyPrivateMethod(int a, int b)
{
// My private method code
}


抱歉,如果我还有其他明显的问题.

感谢Tadit Dash指出了问题.

Milind


Sorry, If I have missed some other obvious question.

Thanks to Tadit Dash for pointing out the issues.

Milind


这篇关于如何从Web方法调用普通方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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