I would like to test the Web API controller method below but I’m not sure how to create a test JSON string for testing in Fiddler because the data type I need to pass to the controller method is a complex type.
public HttpResponseMessage addEmployee([FromBody] JObject empData){
Dictionary<long, Employee> empDetails =
empData["details"].ToObject<Dictionary<long, Employee>>();
int id = empData["id"].ToObject<int>();
string name = empData["name"].ToObject<string>();
string dept = empData["dept"].ToObject<string>();
// create an instance of Employee class
// Call addEmployee method passing in the required parameters
// return HttpResponseMessage Object
}
The Employee class is as below
public class Employee
{
public int Id{get; set;}
public string Name{get; set;}
public string Department{get; set;}
public Dictionary<long, Employee> Details{get; set;}
public void addEmployee(Dictionary<long, Employee> details, int id, string name, string dept)
{
//Add employee
}
}
I would like to create a JSON string that contains a Dictionary composing of a key of type long and a value of type Employee, an id of type int, a name of type string, and a department of type string. I tried to validate the string below in a JSON Validator but it failed. Please point out what I’m missing.
{
"details": {
1:{
"id":12345,
"name":"Tester",
"dept":"Eng"
}
},
"id":12345,
"name":"Tester",
"dept":"Eng"
}