Net Core 3.1 Object null 在 PostAsJsonAsync 之后的 WebApi 方法中
net Core 3.1 Object null in WebApi method after PostAsJsonAsync
我使用此行来使用 API post 方法
var postTask = client.PostAsJsonAsync("AgregarNegocio", new StringContent(JsonConvert.SerializeObject(model).ToString(), Encoding.UTF8, "application/json"));
但是当 API 方法被命中时
public IActionResult AgregarNegocio([FromBody]NegocioViewModel model)
模型中的所有属性均为空...
我已经尝试过使用和不使用 [FromBody] 以及其他解决方案,但是 none 仍然有效,有什么建议吗?谢谢!
尝试使用 PostAsync
而不是 PostAsJsonAsync
var postTask = await client.PostAsync("AgregarNegocio", new StringContent(JsonConvert.SerializeObject(model), Encoding.UTF8, "application/json"));
您可以使用 HttpClient 扩展方法:
https://docs.microsoft.com/en-us/previous-versions/aspnet/hh944682(v=vs.118)
PostAsJsonAsync(
this HttpClient client,
string requestUri,
T value
)
var postTask = client.PostAsJsonAsync<NegocioViewModel>("AgregarNegocio", model);
您可以使用 PostAsync
但也不要忘记按照我在 this article.
中描述的正确方式使用 HttpClient
您需要像这样构建您的 http 客户端:
_client = new HttpClient { BaseAddress = new Uri("your http://my base url goes here"),
Timeout = new TimeSpan(0, 0, 0, 0, -1) };
_client.DefaultRequestHeaders.Accept.Clear();
_client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));//add json header
//_client.DefaultRequestHeaders.Add("Bearer", "some token goes here");
并且您需要像这样调用您的方法:
var postTask = await _client.PostAsJsonAsync("AgregarNegocio", model);
确保在其上调用“await”,因为它是异步的。
备注:
请注意,我添加了 MediaTypeWithQualityHeaderValue 以表明它是 json。
另外使用 Route 通常不是一个好主意...最好使用 HttPost("MyRoute"),因为它结合了 ControllerName + Route。但这取决于你。
我使用此行来使用 API post 方法
var postTask = client.PostAsJsonAsync("AgregarNegocio", new StringContent(JsonConvert.SerializeObject(model).ToString(), Encoding.UTF8, "application/json"));
但是当 API 方法被命中时
public IActionResult AgregarNegocio([FromBody]NegocioViewModel model)
模型中的所有属性均为空... 我已经尝试过使用和不使用 [FromBody] 以及其他解决方案,但是 none 仍然有效,有什么建议吗?谢谢!
尝试使用 PostAsync
而不是 PostAsJsonAsync
var postTask = await client.PostAsync("AgregarNegocio", new StringContent(JsonConvert.SerializeObject(model), Encoding.UTF8, "application/json"));
您可以使用 HttpClient 扩展方法: https://docs.microsoft.com/en-us/previous-versions/aspnet/hh944682(v=vs.118)
PostAsJsonAsync( this HttpClient client, string requestUri, T value )
var postTask = client.PostAsJsonAsync<NegocioViewModel>("AgregarNegocio", model);
您可以使用 PostAsync
但也不要忘记按照我在 this article.
您需要像这样构建您的 http 客户端:
_client = new HttpClient { BaseAddress = new Uri("your http://my base url goes here"),
Timeout = new TimeSpan(0, 0, 0, 0, -1) };
_client.DefaultRequestHeaders.Accept.Clear();
_client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));//add json header
//_client.DefaultRequestHeaders.Add("Bearer", "some token goes here");
并且您需要像这样调用您的方法:
var postTask = await _client.PostAsJsonAsync("AgregarNegocio", model);
确保在其上调用“await”,因为它是异步的。
备注:
请注意,我添加了 MediaTypeWithQualityHeaderValue 以表明它是 json。
另外使用 Route 通常不是一个好主意...最好使用 HttPost("MyRoute"),因为它结合了 ControllerName + Route。但这取决于你。