Postman中如何向asmx Web服务传递参数

3

我创建了一个如下所示的asmx web-service:

[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public void GetEmployeesJSONByID(int sId)
{
  try {
    string sResult = string.Empty;
    List<Employee> lstEmp = new List<Employee>();

    List<Employee> lstEmpNew = new List<Employee>();

    lstEmp.Add(new Employee { Id = 101, Name = "Nitin", Salary = 10000 });
    lstEmp.Add(new Employee { Id = 102, Name = "Dinesh", Salary = 20000 });

    switch (sId) {
      case 101: 
        lstEmpNew.Add(new Employee { Id = 101, Name = "Nitin", Salary = 10000 });
         break;
       case 102:
         lstEmpNew.Add(new Employee { Id = 102, Name = "Dinesh", Salary = 20000 });
         break;
       default:
         break;
     }
     JavaScriptSerializer js = new JavaScriptSerializer();
     sResult = js.Serialize(lstEmpNew);

     Context.Response.Write(sResult);
   } catch (Exception ex) {
     Context.Response.Write(ex.Message.ToString());
   }
 }

我想通过Postman测试这个Web服务。所以我在我的本地IIS上部署了它,并在Postman中进行了如下配置:

enter image description here

我在URL中输入了:

http://XXXXXX/SampleWebService/Service.asmx/GetEmployeesJSONByID?sId=101

我遇到了“请求格式无效”的问题。在postman中如何传递sId来测试此Web服务?


在 web.config 中是否定义了 webservices 以使用 HttpGet? - wazz
1
是的,我们也已经定义了这个。 - VIVEK RAMASAMY
1个回答

2
请将以下内容添加到 Web.config 文件中的 <system.web> 块中:
<webServices>
      <protocols>
        <add name="HttpGet" />
        <add name="HttpPost" />
      </protocols>
</webServices>

问题中的错误图像在Postman中将请求方法设为POST,而源代码中WebMethod的UseHttpGet = true。


这些协议我已经添加到配置文件中了。实际上,这是Get方法,所以我在Postman中将POST更改为GET,它按预期工作了。感谢您的建议。 - VIVEK RAMASAMY
使用以下链接参考:https://www.getpostman.com/docs/v6/postman/sending_api_requests/requests - VIVEK RAMASAMY

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接