Home > Software engineering >  How to get the Message on StatusCodes.Status204NoContent
How to get the Message on StatusCodes.Status204NoContent

Time:10-20

I have this in my controller:

[

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);
            }
        }

I get the response in my Service in Blazor WASM like so:

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}");
            }

I need to get the reply from the controller on "No Content" the message "Cannot Do It!". I am trying the ReasonPhrase, but I don't know how to put the error there.

CodePudding user response:

Warning, not tested.

[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);
      }
}

CodePudding user response:

You can't return any value when you retun NoContext response. This is the only way I see to add your message to a response header. This code was tested using 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);
 }
 .....

action

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