我创建了一个如下所示的 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 中,如下所示:
在我给出的 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/