【问题标题】:How to call a method in webservice如何在 web 服务中调用方法
【发布时间】:2014-05-13 21:15:01
【问题描述】:

我是 Web 服务和 .net 的新手。 我想在我的 Web 服务中从 aspx.cs 文件中调用一个方法。 我该怎么做?

protected void SearchRec(object sender, EventArgs e)
    {
      ....
    }

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void example(string text_to_search)
    {
      .....//call SearchRec here
    }

【问题讨论】:

  • 他们是同一班吗?
  • 你能澄清一下吗?您希望您的代码调用 Web 服务吗?为什么不直接调用函数呢?
  • 这个方法有什么作用?
  • 我希望web服务调用后面代码中的方法
  • 在数据库中搜索特定数据

标签: c# asp.net web-services asmx


【解决方案1】:

如果我理解正确,请写:

public void example(string text_to_search)
{
    this.SearchRec(text_to_search, null);
}

如果它们都在同一个类中声明。

更新: 您必须将 SearchRec 逻辑和其他类似方法转移到单独的类。在您的 aspx.cs 中,您可以使用它创建该类的实例。在 webservice 方法中也是如此。您可以创建该类的实例并调用它的方法。

此外,这些主题也很有用 Difference between webservice, web methods & server side code? , http://msdn.microsoft.com/en-us/library/8wbhsy70(v=vs.90).aspx

【讨论】:

  • 它们没有在同一个类中声明
  • @user3025441 你能创建那个类的实例并调用它的方法吗(你需要将protected改为public才能直接调用它)?
猜你喜欢
  • 1970-01-01
  • 2010-11-16
  • 1970-01-01
  • 1970-01-01
  • 2011-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多