我正在尝试通过JSON将对象发送到web API,但我不断遇到异常
我的客户端操作是:
public string SubmitNewIncident(Incident input)
{
string response = String.Empty;
string serialisedJSON = String.Empty;
input.Type = 0;
serialisedJSON = JsonConvert.SerializeObject(input);
string fpath = String.Format(@"C:\dev\serialisedJSONLog_{0}.txt", DateTime.Now.ToString("yyyy-dd-M--HH-mm-ss"));
System.IO.File.WriteAllText(fpath, serialisedJSON);
using (WebClient wc = new WebClient())
{
wc.Headers[HttpRequestHeader.ContentType] = "application/json";
try
{
response = wc.UploadString(new Uri("http://localhost:25657/api/RaiseNew"), serialisedJSON);
}
catch(Exception ex)
{
string path = String.Format(@"C:\dev\ErrorLog_{0}.txt", DateTime.Now.ToString("yyyy-dd-M--HH-mm-ss"));
System.IO.File.WriteAllText(path, ex.ToString());
throw ex;
}
}
return (response);
}
JSON字符串看起来不错,但是很长。我的服务器端代码是:
public class RaiseNewController : ApiController
{
// GET api/raisenew
[HttpGet]
public HttpStatusCode Get()
{
return HttpStatusCode.OK;
}
//POST api/raisenew
[HttpPost]
public int Post([FromBody] Incident input)
{
input.AssignedTo = AssignNewTicket(input.AppID ?? 0);
return 0;
}
字符串serialisedJSON
的值太长,无法在此处发布
当我调用该操作并上传JSON字符串时,我得到了以下异常:
System.Net.WebException: The remote server returned an error: (500) Internal Server Error.
发布于 2017-11-15 12:04:32
我认为您要做的就是告诉ASP.Net在POSTed数据中查找一个名为value
的属性。
我个人会让ASP.Net自动处理反序列化。
[HttpPost]
public int Post(Incident incident)
{
Process(incident);
return 0;
}
https://stackoverflow.com/questions/47306750
复制