我正在尝试使用Post方法以JSON格式传递原始数据,并将其存储在具有属性但显示错误的类中。
我在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);
}
类我想在
中存储数据的位置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; }
}
我在Postman Post http://localhost:50278/api/values/AddEmployee
中传递的命令带数据
[
{
"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
}
]
我收到以下错误:
System.dll中发生System.Net.WebException
但未在用户代码中处理。
其他信息:
The remote server returned an error: (405) Method Not Allowed.
您能否告诉我们为什么显示错误以及日期格式是否正确?
答案 0 :(得分:1)
你试过这个吗?
[HttpPost]
public void AddEmployee([FromBody]Models.Employee user)
{
EService eserv = new EService();
eserv.Addemp(user);
}