ASMX Web 服务和 HTTP GET

发布于 2024-09-07 04:56:18 字数 675 浏览 2 评论 0原文

我正在尝试创建一个可以执行 HTTP GET 请求的 ASMX Web 服务。我有以下简单的代码片段来说明我已经完成的工作。

using System.Web.Script.Services;
...

[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public string HelloWorld(HttpContext context)
{
 return context.Request.Params.Get("userId").ToString();
}

除此之外,我还在我的 Web.config 文件中添加了以下节点。

<webServices>
  <protocols>
    <add name="HttpGet"/>
    <add name="HttpPost"/>
  </protocols>
</webServices>

我面临的问题是我不断收到可怕的“System.Web.HttpContext 无法序列化,因为它确实每当我尝试调试此 Web 服务时,没有无参数构造函数”错误消息。我不知道问题是什么,我非常感谢您为我摆脱困境提供的任何帮助。我意识到 HTTP GET 请求应该非常简单,但我真的不确定我的挫败感是什么。

I'm trying create a ASMX webservice that can perform a HTTP GET request. I have the following simple snippet of code to illustrate what I've already done.

using System.Web.Script.Services;
...

[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public string HelloWorld(HttpContext context)
{
 return context.Request.Params.Get("userId").ToString();
}

In addition to this, I've also added the following nodes in my Web.config file

<webServices>
  <protocols>
    <add name="HttpGet"/>
    <add name="HttpPost"/>
  </protocols>
</webServices>

The problem that I'm facing is that I'm constantly getting the dreaded "System.Web.HttpContext cannot be serialized because it does not have a parameterless constructor" error message whenever I try to debug this webservice. I have no idea what the problem is, and I would really appreciate any assistance that is offered to get me out of this quandary. I realize that HTTP GET requests are supposed to be very simple, but I'm really uncertain of what the cause of my frustrations are.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挖个坑埋了你 2024-09-14 04:56:18

我认为您希望

[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public string HelloWorld(int userId)
{
    return userId.ToString();
}

您可以在函数签名中指定参数,并且如果需要,您可以将 HttpContext 作为 Context (基类 WebService 上的属性)访问。

I think you want

[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public string HelloWorld(int userId)
{
    return userId.ToString();
}

You can specify parameters in the function signature and you can access the HttpContext as Context (a property on the base class WebService) if you need it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文