代码之家  ›  专栏  ›  技术社区  ›  Paul

来自PostAsync的内部服务器错误

  •  1
  • Paul  · 技术社区  · 7 年前

    我有一个方法可以在下面将数据发布到一个URL

        public async Task<IActionResult> PostAsync(string method, string data, bool isJson = true, long key = 0)
        {
            IActionResult result;
            try
            {
                var proxyUrl = await EstablishProxyUrlAsync(method, key).ConfigureAwait(false);
                var content = isJson ? new StringContent(data, Encoding.UTF8, "application/json") : new StringContent(data);
    
                var response = await this._httpClient.PostAsync(proxyUrl, content).ConfigureAwait(false);
                result = await ProcessResponseAsync(response).ConfigureAwait(false);
            }
            catch (Exception e)
            {
                Log.Error(e, this.GetType().Name + ": Error in PostAsync");
                throw;
            }
    
            return result;
        }
    

    你可以看到我 设置ContentType时,关于处理StringContent的文章负载说明了如何使用此方法。

    不过,我刚把这个拿回来

    {StatusCode: 500, ReasonPhrase: 'Internal Server Error', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
    {
      Date: Sun, 29 Jul 2018 19:19:35 GMT
      Server: Kestrel
      Content-Length: 0
    }}
    

    从发现问题的角度来看,这显然是一种无用的反应。

    正在调用的方法如下

    [HttpPost]
    [ActionName("Add")]
    public async Task<IActionResult> AddAsync(StringContent content)
    {
        var myJson= await content.ReadAsStringAsync().ConfigureAwait(false);
        var object= JsonConvert.DeserializeObject<MyObject>(myJson);
        var result = await _service.AddAsync(object).ConfigureAwait(false);
    
        return result;
    }
    

    如你所见,我已经包括了httppost

    有人知道什么会导致这个吗?

    我使用的是服务结构,这个URL在一个分区上,但我认为这不是问题,因为这个路由在其他区域也可以工作。

    保罗

    1 回复  |  直到 7 年前
        1
  •  3
  •   Nkosi    7 年前

    [HttpPost]
    [ActionName("Add")]
    public async Task<IActionResult> AddAsync([FromBody] MyObject model) {
        var result = await _service.AddAsync(model);
        return result;
    }
    

    _service.AddAsync(model); IActionResult

    Model Binding in ASP.NET Core