0

From .NET Core I return object in Ok() in method with IActionResult result:

 public async Task<IActionResult> UploadJsonFiles()
{
      return Ok(new { status = "Ok", message = "" });
}

In Angular I have that json but it is formatted as string. I don't have object with property status and message, only string:

this.uploader.response.subscribe(res => {
..

res = "{"status":"Ok","message":""}"

In Startup I have set formatters for controllers:

services.AddControllersWithViews().AddNewtonsoftJson();
services.AddControllers(options =>
{
    options.RespectBrowserAcceptHeader = true; // false by default
})
.AddJsonOptions(options =>
        options.JsonSerializerOptions.PropertyNamingPolicy = null);
5
  • 1
    Does this answer your question? How to have Angular's HttpClient return an object instead of a string? Commented Feb 2, 2021 at 13:52
  • Not exaclty, because why my api return string when I use Ok(), it should return json to the Angular service, I have set formatters in Startup. Maybe I should have another one? Commented Feb 2, 2021 at 14:58
  • 1
    If you are using Chrome, have you taken a look in Developer Tools -> Network tab? There you can examine what is sent from the browser and received from the server. (If you are not using Chrome you can download Fiddler which will let you trace the rest calls.) Take a look the body of your response. Is it a string? Take a look at the accept header in your request. It should say application/json. Take a look at Content-Type header in the response. It should also say application/json Commented Feb 2, 2021 at 15:14
  • Following mortb's comment, you can specify the response type is json by using [Produces("application/json")] attribute on the method. Commented Feb 3, 2021 at 1:48
  • 1
    As far as I know, the OK result will return json result by default. I suggest you could try to use return Json(new { status = "Ok", message = "saaaaaa" }); and try again. Commented Feb 3, 2021 at 8:30

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.