c# - 将参数传递给 Postman 中的 asmx 网络服务

标签 c# asp.net web-services postman

我创建了一个如下所示的 asmx 网络服务:

[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 测试这个网络服务。所以我部署在本地 IIS 和 postman 中,如下所示:

enter image description here

在我给出的 URL 中

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

我收到请求格式无效。如何在 postman 中传递这个sId来测试这个webservice?

最佳答案

将以下内容添加到 <system.web>在 Web.config 文件中阻止:

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

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

关于c# - 将参数传递给 Postman 中的 asmx 网络服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51057117/

相关文章:

asp.net - 如何使用VS2010一键发布(MsDeploy)从命令行进行远程部署?

android - 下载 XML 文件时出现异常

javascript - 无法修改 Controls 集合,因为该控件包含代码块(即 <% ...%>)ajax 工具包 html 编辑器

asp.net - 零星错误 : The file has not been pre-compiled, 且无法请求

web-services - 如何为 SOAP Web 服务生成 XSD

javascript - CoffeeScript 服务器不如 Node.js 服务器可靠吗?

c# - Lotus Note PassThruHTML

c# - 使用按钮单击 c# 在列表框中选择多个项目

c# - EF 返回的 FK 集合为空,但验证重复项,确认就是这样,集合有内容

c# - 在屏幕上检索凝视坐标