【问题标题】:Error CS1525: Invalid expression term 'string'错误 CS1525:无效的表达式术语“字符串”
【发布时间】: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


【解决方案1】:

欢迎使用 StackOverflow Cosmin Mihalache。

我认为使用您的变量 jsonModel 而不是新的 StringContent 可能会对您有所帮助:

var response = await _client.PostAsync(new Uri(
                   $"{_baseAddress}/create"),
                   jsonModel
               )).ConfigureAwait(false);

【讨论】:

    猜你喜欢
    • 2011-06-22
    • 2020-09-23
    • 2014-06-01
    • 2016-07-03
    • 1970-01-01
    • 2023-03-30
    • 2015-12-04
    • 2019-09-12
    • 1970-01-01
    相关资源
    最近更新 更多