将数据从dotnetcore api发布到.net api不起作用

时间:2017-05-11 07:22:20

标签: c# .net api .net-core

我们正在DDD(域驱动设计)环境中工作,我们有几十个用.Net制作的API。我们正在创建的最新网站是一个dotnetcore网站,我们想要调用所有旧的.Net API:s。问题是我无法获得旧API的任何值。每次我向旧API发布内容时它都会变为null。当我创建一个dotnetcore api它工作得很好。 dotnetcore是否存在调用旧的.net API的已知问题?

OLD API:

          [HttpPost]
    [Route("GetIllustration")]
    public async Task<HttpResponseMessage> GetIllustration([FromBody] JsData illustrationRequest)
    {
        if(illustrationRequest == null)
        {
            return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Request was null");
        }
        //var modelService = modelLookupService.Get(illustrationRequest.ModelName);
        //var illustration = modelService.Get(illustrationRequest);
        return Request.CreateResponse(HttpStatusCode.Accepted, illustrationRequest);
    }

使用dotnetcore:

          [HttpPost]
    [Route("GetIllustration")]
    public IActionResult GetIllustration([FromBody] JsData illustrationRequest)
    {
        if (illustrationRequest == null)
        {
            return BadRequest(illustrationRequest);
        }
        return Ok(illustrationRequest);
    }

返回类型不同,但在不同类型的API中它是相同的方法。

编辑1:

致电.net api:

 public IActionResult Get(GetIllustrationRequest request)
      {
        var req = new JsData()
        {
            CountryCode = "DE",
            DrawSideInfo = true,
            IlXml = "XML",
            ImageSizeHeight = 123,
            ImageSizeWidth = 1234,
            LargeImageSizeHeight = 12345,
            LargeImageSizeWidth = 123456,
            ModelName = "SUPERIOR",
            ShowInside = true,
            SupplierName = "NORMSTAHL",
            TopItemName = "SUPERIOR+42",
            XposText = "HAJ",
            YposText = "DÅÅ"
        };
        return PostAsJson("/GetIllustration", req);
    }

enter image description here

对dotnetcore api的工作电话:

   public IActionResult Get(GetIllustrationRequest request)
    {
        var req = new JsData()
        {
            CountryCode = "DE",
            DrawSideInfo = true,
            IlXml = "XML",
            ImageSizeHeight = 123,
            ImageSizeWidth = 1234,
            LargeImageSizeHeight = 12345,
            LargeImageSizeWidth = 123456,
            ModelName = "SUPERIOR",
            ShowInside = true,
            SupplierName = "NORMSTAHL",
            TopItemName = "SUPERIOR+42",
            XposText = "HAJ",
            YposText = "DÅÅ"
        };
        return PostAsJson("api/Configuration/GetIllustration", req);
    }

enter image description here

编辑2:

这是实际的邮政编码:

   using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(BaseUri);
                var cancellationToken = new CancellationToken();
                client.DefaultRequestHeaders.Add("X-Content-Type-Options", "nosniff");                 
                if (!string.IsNullOrEmpty(OAuth2Token))
                {
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
                        "BEARER",
                        OAuth2Token);
                }
                var response = client.PostAsJsonAsync(path, value, cancellationToken).Result;
                var stringResult = response.Content.ReadAsStringAsync();
                var result = JsonConvert.DeserializeObject<object>(stringResult.Result);
                return Ok(result);
            }

1 个答案:

答案 0 :(得分:1)

显然,dotnetcore由于某种原因无法处理postasjsonasync方法。所以我不得不手动改变我的方法:

                var content = JsonConvert.SerializeObject(value);
                HttpContent httpContent = new StringContent(content, Encoding.UTF8, "application/json");
                var response = client.PutAsync(path , httpContent, cancellationToken).Result;
                var stringResult = response.Content.ReadAsStringAsync();

而不是:

            var response = client.PutAsJsonAsync(path, value, cancellationToken).Result;
            var stringResult = response.Content.ReadAsStringAsync();
            var result = JsonConvert.DeserializeObject<object>(stringResult.Result);