首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >JSON到ASP.NET Web API错误:(500)内部服务器错误

JSON到ASP.NET Web API错误:(500)内部服务器错误
EN

Stack Overflow用户
提问于 2017-11-15 19:54:14
回答 1查看 1.7K关注 0票数 0

我正在尝试通过JSON将对象发送到web API,但我不断遇到异常

我的客户端操作是:

代码语言:javascript
运行
复制
    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字符串看起来不错,但是很长。我的服务器端代码是:

代码语言:javascript
运行
复制
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字符串时,我得到了以下异常:

代码语言:javascript
运行
复制
    System.Net.WebException: The remote server returned an error: (500) Internal Server Error.
EN

回答 1

Stack Overflow用户

发布于 2017-11-15 20:04:32

我认为您要做的就是告诉ASP.Net在POSTed数据中查找一个名为value的属性。

我个人会让ASP.Net自动处理反序列化。

代码语言:javascript
运行
复制
[HttpPost]
public int Post(Incident incident)
{
    Process(incident);

    return 0;
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47306750

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档