Skip to main content
 首页 » 编程设计

c#之ASMX 网络服务和 HTTP GET

2024年02月24日25mayingbao

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

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> 

我面临的问题是,每当我尝试调试此网络服务。我不知道问题出在哪里,如果能帮助我摆脱困境,我将不胜感激。我意识到 HTTP GET 请求应该非常简单,但我真的不确定是什么原因让我感到沮丧。

请您参考如下方法:

我觉得你想要

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

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