【发布时间】:2020-05-07 15:00:26
【问题描述】:
我有这个代码:
var advertApiModel = _mapper.Map<AdvertModel>(model);
var jsonModel = JsonConvert.SerializeObject(advertApiModel);
var response = await _client.PostAsync(new Uri(
$"{_baseAddress}/create"),
new StringContent(jsonModel, Encoding.UTF8,
"application/json"
)).ConfigureAwait(false);
var createAdvertResponse = await response.Content.ReadAsAsync<CreateAdvertResponse>().ConfigureAwait(false);
var advertResponse = _mapper.Map<AdvertResponse>(createAdvertResponse);
当我运行它并跟踪它时,它在 var 响应中不断收到错误 Invalid expression term 'string' 并且终端显示:
信息:System.Net.Http.HttpClient.IAdvertApiClient.ClientHandler[101] 在 246.85940000000002 毫秒后收到 HTTP 响应 - InternalServerError System.Net.Http.HttpClient.IAdvertApiClient.ClientHandler:信息:在 246.85940000000002 毫秒后收到 HTTP 响应 - InternalServerError
【问题讨论】:
-
显然您发送的请求会导致服务器出现错误。在不知道 API 及其期望(以及您发送的具体内容)的情况下,我怀疑有什么可以帮助您的。
-
"无效的表达式术语 'string'" - 这在哪里显示?
-
@CodeCaster 它显示当我将鼠标悬停在编码上时。当它尝试创建 StringContent 时,它会读取 jsonModel 但是当我将鼠标悬停在“application/json”上时会给出该错误,然后一切都会中断
-
删除
new StringContent并改用jsonModel变量。
标签: c# asp.net-mvc api httpclient