0
votes

Is there a way to return a ReasonPhrase and some plain text from aspnet core to Angular? I create a new record and return it's id to the client, along with a ReasonPhrase for Toastr.

I have this on my webapi controller:

    [HttpPut("save")]
    public async Task<IActionResult> Save(MyModel model)
    {
        model.Id = await _service.Save(model);

        return Ok(new HttpResponseMessage
        {
            ReasonPhrase = $"Record saved",
            Content = new StringContent(model.Id.ToString(), Encoding.UTF8, "text/plain"),
        });...

And when I try to read it the content in Angular (8) like so:

save$.subscribe((result: any) => {
      console.log(result.content);

I see this:

{headers: Array(1)}
headers: Array(1)
0:
Key: "Content-Type"
Value: ["text/plain; charset=utf-8"]
__proto__: Object
length: 1
__proto__: Array(0)
__proto__: Object

No idea what I'm doing wrong (is it even possible to do this?) but I want the value to be my newly created id but it's ["text/plain; charset=utf-8"] instead

1

1 Answers

1
votes

Is there a way to return a ReasonPhrase and some plain text from aspnet core to Angular?

To achieve requirement, you can refer to the following code snippet using an anonymous type with two properties named ReasonPhrase and Content, like below.

[HttpPut("save")]
public async Task<IActionResult> Save(MyModel model)
{
    //...

    return Ok(new
    {
        ReasonPhrase = $"Record saved",
        Content = model.Id.ToString()
    });
}

On Angular client

.subscribe((result: any) => {
  console.log(result);
  console.log(result.reasonPhrase);
  console.log(result.content);

  // ...
  // code logic here
  // ...
});

Test Result

enter image description here