C# 当我发出http put请求时,内容总是空的
我有一个有效的web API。现在我正在为它编写集成测试,但是当我通过测试发出PUT请求时,C# 当我发出http put请求时,内容总是空的,c#,.net,http,asp.net-core,dotnet-httpclient,C#,.net,Http,Asp.net Core,Dotnet Httpclient,我有一个有效的web API。现在我正在为它编写集成测试,但是当我通过测试发出PUT请求时,[FromBody]数据总是空的RequestData是我创建的一个protobuf类型 这是API方法的签名: public async Task<Response> SomePutMethod(string id, [FromBody] RequestData data) 我还尝试了Jsonformatter.Default.Format将protobuf对象转换为json字符串,但也没
[FromBody]
数据总是空的RequestData
是我创建的一个protobuf类型
这是API方法的签名:
public async Task<Response> SomePutMethod(string id, [FromBody] RequestData data)
我还尝试了
Jsonformatter.Default.Format
将protobuf对象转换为json字符串,但也没有成功。使用System.Net.Http.Formatting.HttpClientExtensions中的扩展名PutAsJsonAsync()
:
var data = new RequestData();
var resp = await _client.PutAsJsonAsync("https://www.example.com", data);
或者,使用SendAsync()
我通过测试发出PUT请求,[FromBody]
数据始终为空
请检查您的RequestData
类,并确保您定义了公共属性,如下所示
public class RequestData
{
public string Content { get; set; }
//...
//other properties
如果将它们定义为没有get
和set
访问器的字段,则可能会导致问题
此外,在创建RequestData
类的实例并使用该request
对象填充StringContent
时,请检查是否为每个属性分配了值
var request = new RequestData();
request.Content = "hello world";
//...
它仍然是空的。两种方式如果端点被击中并且主体为空,则您的模型不匹配。真的没有其他方法来完成你想做的事情。在你的问题中,你说过端点是有效的,只是在编写一个测试来使用它的时候不起作用。那么为什么它们不匹配呢??我做错了什么?嗨@an007,有关于这个案子的最新消息吗?
public class RequestData
{
public string Content { get; set; }
//...
//other properties
var request = new RequestData();
request.Content = "hello world";
//...