C#.NET WEBAPI 接口设计与调用
作者:C/S框架网  发布日期:2017-09-20 08:57:21
  C#.NET WEBAPI 接口设计与调用



客户端传递参数字符串,调用FromBody参数的接口
 

C# Code:

[HttpPost]
[Route("post-frombody")]
public string PostFromBody([FromBody]string data)
{
   return "返回结果:[FromBody]仅支持定义一个参数!!!" +data;
}

//来源:C/S框架网(www.csframework.com) QQ:1980854898



调用:


C# Code:

string url = "http://localhost:8899/api/testapi/post-frombody";

//服务端:public string Post([FromBody] string value) //[FromBody]仅支持定义一个参数!
//则客户端Post的数据:拼接的字符串必须以 = 开头,否则服务端无法取得value。例如:=rfwreewr2332322232 或者 {'':value }
//Content-Type:application/x-www-form-urlencoded;charset=UTF-8
string param = "=this is a data!";//OK
//string param = "{'':this is a data}";//OK

string result = WebApiTools.Post(url, param, "application/x-www-form-urlencoded;charset=UTF-8");
txtResponse.Text = result;

//来源:C/S框架网(www.csframework.com) QQ:1980854898




客户端传递JSON字符串,调用动态对象接口



C# Code:

[HttpPost]
[Route("post-dynamic")]
public string PostStr(dynamic data)
{
   return "返回结果: "+ data.data1+ "|" +data.data2;
}

//来源:C/S框架网(www.csframework.com) QQ:1980854898



调用:



C# Code:

string url = "http://localhost:8899/api/testapi/post-dynamic";
string param = "{\"data1\":11111111,\"data2\":2222222}";//OK
string result = WebApiTools.Post(url, param, "application/json;charset=UTF-8");
txtResponse.Text = result;

//来源:C/S框架网(www.csframework.com) QQ:1980854898


上一篇 下一篇