Tamil Iniyan

Tamil Iniyan

  • NA
  • 79
  • 7.6k

how to post web api frombody parameter together with simple

Mar 10 2017 1:37 AM
I am trying to pass the id and employeemodel object in below format but it throws an error like method not allowed
 
string.Format("id=1007&EmployeeID={0}&EmployeeName={1}&EmployeeDept={2}&EmployeeEmail={3}&EmployeePhone={4}", 1007, "guna", "HR", "[email protected]", "9566764152")
WEB API METHOD:
public void Put(int id, [FromBody]EmployeeModel value)
{
employeeRepository.UpdateEmployee(id, value);
}
class: 
public class EmployeeModel
{
[Key]
public int EmployeeID { get; set; }
[Required(ErrorMessage = "Enter Name")]
public string EmployeeName { get; set; }
public string EmployeeDept { get; set; }
public string EmployeeEmail { get; set; }
public string EmployeePhone { get; set; }
}
 
Client request: 
 
WebResponse response;
HttpWebRequest request;
string postString = string.Format("id=1007&EmployeeID={0}&EmployeeName={1}&EmployeeDept={2}&EmployeeEmail={3}&EmployeePhone={4}", 1007, "guna", "HR", "[email protected]", "9566764152");
request = (HttpWebRequest)WebRequest.Create("http://localhost:6267/api/employeevalue");
request.Accept = "application/json";
request.Method = "PUT";
request.ContentType = "application/x-www-form-urlencoded";
StreamWriter requestWriter = new StreamWriter(request.GetRequestStream());
requestWriter.Write(postString);
requestWriter.Close();
response = (HttpWebResponse)request.GetResponse(); // geting method not allowed error.

Answers (4)