简体   繁体   English

C# HttpClient - PutAsync

[英]C# HttpClient - PutAsync

I have a backend controller method我有一个后端控制器方法

[Route("{ropeId}/segments")]
[HttpPut]
public async Task<IActionResult> Put([NotNull] [FromBody] IEnumerable<Segment> segments)
{
    Debug.WriteLine("In here!");
}

I can hit this fine using a scratch pad in Rider:我可以使用 Rider 中的便笺簿击中此罚款:

PUT https://localhost:6001/api/ropes/3/segments
Accept: */*
Cache-Control: no-cache
Content-Type: application/json

[{"SegmentNo":1,"RopeId":3,"LastReceived":"2019-12-28T14:34:35.9799039+01:00","BendDamage":0.018,"Temperature":30.0,"LoadLevel":1},
{"SegmentNo":16,"RopeId":3,"LastReceived":"2019-12-28T14:34:35.9799039+01:00","BendDamage":0.018,"Temperature":30.0,"LoadLevel":1}, {"SegmentNo":50,"RopeId":3,"LastReceived":"2019-12-28T14:34:35.9799039+01:00","BendDamage":0.018,"Temperature":30.0,"LoadLevel":1},
{"SegmentNo":69,"RopeId":3,"LastReceived":"2019-12-28T14:34:35.9799039+01:00","BendDamage":0.018,"Temperature":30.0,"LoadLevel":1},
{"SegmentNo":101,"RopeId":3,"LastReceived":"2019-12-28T14:34:35.9799039+01:00","BendDamage":0.018,"Temperature":30.0,"LoadLevel":1},  {"SegmentNo":106,"RopeId":3,"LastReceived":"2019-12-28T14:34:35.9799039+01:00","BendDamage":0.018,"Temperature":30.0,"LoadLevel":1},
{"SegmentNo":128,"RopeId":3,"LastReceived":"2019-12-28T14:34:35.9799039+01:00","BendDamage":0.018,"Temperature":30.0,"LoadLevel":1}]

The Segment Model:细分模型:

public sealed class Segment
{
   public Segment(short segmentNo,
                  byte ropeId,
                  DateTime lastReceived,
                  double bendDamage,
                  double temperature,
                  byte loadLevel)
   {
      SegmentNo = segmentNo;
      RopeId = ropeId;
      LastReceived = lastReceived;
      BendDamage = bendDamage;
      Temperature = temperature;
      LoadLevel = loadLevel;
   }

   public short SegmentNo { get; }
   public byte RopeId { get; }
   public DateTime LastReceived { get; }
   public double BendDamage { get; }
   public double Temperature { get; }
   public byte LoadLevel { get; }
}

I can't hit it with a httpClient PutAsync call.我无法使用 httpClient PutAsync 调用来命中它。

I've tried:我试过了:

var httpContent = new StringContent(updatedSegments);
var response = await httpClient.PutAsync(@"https://localhost:6001/api/ropes/3/segments", httpContent);

But I get a SerializeError.但是我得到了一个 SerializeError。

Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1]
Route matched with {action = "Put", controller = "Ropes"}. Executing action WEBAPI.Controllers.RopesController.Put (WEBAPI)
info: Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor[1]
Executing ObjectResult, writing value of type 'Microsoft.AspNetCore.Mvc.SerializableError'.

What is wrong with my PutAsync call?我的 PutAsync 调用有什么问题?

Like I said, this works in Postman / PUT scratch.就像我说的,这适用于 Postman / PUT scratch。

If I do如果我做

var s = await httpContent.ReadAsStringAsync();
s.Print();

I get the JSON above, so I think the issue is I need to set a header somehow on the httpContent to tell it that it is application/json?我得到了上面的 JSON,所以我认为问题是我需要在 httpContent 上以某种方式设置一个标头来告诉它它是 application/json?

Have you tried to specify the content type on the constructor for the httpContent object?您是否尝试在构造函数上为 httpContent 对象指定内容类型? I worked with httpClient call and declaring the type worked for me like so:我使用 httpClient 调用并声明该类型对我有用,如下所示:

var httpContent= new StringContent(updatedSegments.ToString(Newtonsoft.Json.Formatting.None), Encoding.UTF8, "application/json");

Let me know if it worked for you as well让我知道它是否也适合你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM