Reputation: 57
I am trying to pass raw data in JSON format using Post method and store it in class which is having properties but is showing error.
My code in Value Controller-
[HttpPost]
public void AddEmployee()
{
HttpWebRequest myHttpWebRequest`= (HttpWebRequest)WebRequest.Create("http://localhost:50278/api/values/AddEmployee");
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
////////Above line is giving error///////
Stream receiveStream = myHttpWebResponse.GetResponseStream();
DataContractJsonSerializer serializer =new DataContractJsonSerializer(typeof(Models.Employee));
Models.Employee user = (Models.Employee)serializer.ReadObject(receiveStream);
}
Class Where I want to store my data in
Models.Employee
{
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public Nullable<System.DateTime> DOB { get; set; }
public Nullable<System.DateTime> DOJ { get; set; }
public Nullable<short> IsActivce { get; set; }
}
Command which i passed in Postman Post http://localhost:50278/api/values/AddEmployee
with data
[
{
"FirstName" : "ABDCD",
"MiddleName" :"sajsoa",
"LastName" : "KSJFHF",
"DOB":1994-01-12 00:00:00.0001994-01-12 00:00:00.000,
"DOJ":1994-01-12 00:00:00.0001994-01-12 00:00:00.000,
"Isactive" : 1
}
]
I received the below error:
System.Net.WebException
occurred in System.dll but was not handled in user code.
Additional information:
The remote server returned an error: (405) Method Not Allowed.
Can you please tell why is it showing error and whether the date format is correct?
Upvotes: 1
Views: 3242
Reputation: 7165
Have you tried this?
[HttpPost]
public void AddEmployee([FromBody]Models.Employee user)
{
EService eserv = new EService();
eserv.Addemp(user);
}
Upvotes: 1