如何获取有关 StatusCodes.Status204NoContent 的消息

How to get the Message on StatusCodes.Status204NoContent

我的控制器里有这个:

[

HttpPost]
        public async Task<ActionResult> UpdateCalendarEntry(CalendarEntry model)
        {
            try
            {
                model.LabelColor = "blue";
                var result = await repo.AddCalendarEntry(model);
                if(result == null)
                {
                    return StatusCode(StatusCodes.Status204NoContent, "Cannot Do It!");
                }
                return apiResult.Send200(result);
            }
            catch (Exception ex)
            {

                return apiResult.Send400(ex.Message);
            }
        }

我在 Blazor WASM 中的服务中得到这样的响应:

using var response = await httpClient.SendAsync(request);
            var content = await response.Content.ReadAsStringAsync();

            // auto logout on 401 response
            if (response.StatusCode == HttpStatusCode.Unauthorized)
            {
                navigationManager.NavigateTo("login");
                return default;
            }

            if(response.StatusCode == HttpStatusCode.BadRequest)
            {
                await helperService.InvokeAlert("Bad Request", $@"{response.ReasonPhrase}", true);
            }

            if(response.StatusCode == HttpStatusCode.NoContent)
            {
                var x = response.Content.ReadAsStringAsync();
                await helperService.InvokeAlert("Bad Request", $@"{response.ReasonPhrase}", true);
            }

            // throw exception on error response
            if (!response.IsSuccessStatusCode)
            {
                //var error = await response.Content.ReadFromJsonAsync<Dictionary<string, string>>();
                //throw new (error["message"]);

                return default;
                //throw new ApplicationException
                //    ($"The response from the server was not successful: {response.ReasonPhrase}, " +
                //    $"Message: {content}");
            }

我需要从控制器那里得到关于“无内容”的回复消息“不能做!”。我正在尝试 ReasonPhrase,但我不知道如何将错误放在那里。

警告,未经测试。

[HttpPost]
public async Task<ActionResult> UpdateCalendarEntry(CalendarEntry model)
{
      try
      {
            model.LabelColor = "blue";
            var result = await repo.AddCalendarEntry(model);
            if(result == null)
            {
                 return new ObjectResult("Cannot Do It!")
                 {
                      StatusCode = HttpStatusCode.NoContent
                 };
            }
            return apiResult.Send200(result);
      } 
      catch (Exception ex)
      {
            return apiResult.Send400(ex.Message);
      }
}

当您重新调整 NoContext 响应时,您不能 return 任何值。这是我看到的将您的消息添加到响应中的唯一方法 header。此代码已使用 VS

进行测试
....
 if(response.StatusCode == HttpStatusCode.NoContent)
 {
   var reason= response.Headers.FirstOrDefault(h=> h.Key=="Reason");
   if(reason!=null)
    await helperService.InvokeAlert("Bad Request", $@"{reason.Value}", true);
 }
 .....

行动

HttpPost]
 public async Task<ActionResult> UpdateCalendarEntry(CalendarEntry model)
 {
            .....
  if(result == null)
  {
 HttpContext.Response.Headers.Add("Reason", "Cannot Do It!");
return NoContent();
  }
}