【发布时间】:2016-10-03 04:06:27
【问题描述】:
问题说明:
资源 URI:
address/index.php?r=api/employee请求头:
Content- Type: application/jsonHTTP 方法:
POST请求正文:
{ "employeeName" : "ABC","age":"20","ContactNumber": "12341234"}
上述参数应作为 JSON 字符串中的一行 HTTP POST 传递给系统。
我正在尝试使用RESTSharp 解决这个问题。但是我遇到了一些问题,比如在执行请求后,我的代码返回 Null 响应,我不确定我的 JSON 字符串是否正确传递。
这是我的代码:
public ActionResult EmployeeInfo(Employee employee)
{
//string empName = unSubscription.employeeName.ToString();
var client = new RestClient("http://localhost:21779/");
var request = new RestRequest("api/employee ", Method.POST);
request.RequestFormat = DataFormat.Json;
request.AddBody(new Employee
{
employeeName = "ABC",
age = "20",
ContactNumber = "12341234"
});
request.AddHeader("Content-Type", @"application/json");
// execute the request
IRestResponse response = client.Execute(request);
var content = response.Content; // raw content as string
return View();
}
我的代码有什么问题吗??
我有点困惑
request.AddUrlSegment("username", "Admin") and request.AddParameter("name", "value").
基本上我想知道如何使用AdduUrlSegment() 和AddParameter()。
提前致谢。
【问题讨论】:
标签: asp.net-mvc rest asp.net-web-api json.net restsharp