Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

OkObjectResult with string value outputs plain text only, no json. #6646

Closed
ErrorEater opened this issue Aug 11, 2017 · 11 comments
Closed

OkObjectResult with string value outputs plain text only, no json. #6646

ErrorEater opened this issue Aug 11, 2017 · 11 comments
Assignees

Comments

@ErrorEater
Copy link

ErrorEater commented Aug 11, 2017

[HttpGet]
public IActionResult GetInfo()
{
    var info = "Hello";

    return Ok(info);
}

The method above always returns plain text (Hello) content instead of ("Hello") even if the request has "Accept: application/json" header.

@rynowak
Copy link
Member

rynowak commented Aug 11, 2017

Is the response "Hello" or Hello? The former is valid JSON.

@ErrorEater
Copy link
Author

There response is always: Hello
I expect: "Hello"

@Eilon Eilon added this to the 2.1.0 milestone Aug 15, 2017
@Eilon
Copy link
Member

Eilon commented Aug 15, 2017

@dougbu if this looks like a bad regression we should consider patching it, so let me know.

@dougbu
Copy link
Member

dougbu commented Aug 15, 2017

I'm unable to reproduce this with ASP.NET Core, testing both the released 2.0.0 and current dev. @aliveli186 please provide a repro for what you've described, preferably in a GitHub repo.

What I'm seeing

For example the PowerShell command curl -headers @{ Accept = 'text/json' } http://localhost:2092/Home/GetInfo shows the service returns "Hello" in the body -- including the quotation marks. The returned Content-type header correctly matches the request. And, the same thing works when requesting application/json.

The action returns bare Hello with Content-Type: text/plain only if the Accept header

  • is missing,
  • includes text/plain,
  • includes a broader wildcard e.g. */*, or
  • includes only unsupported content types e.g. just application/xml when XML formatters are not enabled.

@rynowak
Copy link
Member

rynowak commented Aug 16, 2017

@aliveli186 - what's your startup code look like (just need the ConfigureServices). Are you using AddMvc()?

@ErrorEater
Copy link
Author

ErrorEater commented Aug 16, 2017

@rynowak Yes I'm using AddMvc().

services.AddMvc()
        .AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());

AddJsonOptions has no effect on the result.

@ErrorEater
Copy link
Author

ErrorEater commented Aug 16, 2017

@dougbu By the way this happens in: netcoreapp1.1
Couldn't test it in netcoreapp2.0 because IIS Express in not launching somehow.
Fiddler screenshot of the request and response: http://imgur.com/0IHSnps

@dougbu
Copy link
Member

dougbu commented Aug 16, 2017

Ah, that explains why I can't hit this using 2.0.0! You're hitting #4945 and we fixed that issue in 2.0.0.

The 1.1.x workaround is to add the following to the AddMvc() call in your ConfigureServices() method:

services.AddMvc(options => options.OutputFormatters.RemoveType<StringOutputFormatter>());

@Eilon I suspect the behaviour changes we made for #4945 were thought too significant for a patch. But, perhaps we should reconsider? (The recent #6657 issue also touched on the new behaviour.)

@Eilon
Copy link
Member

Eilon commented Aug 16, 2017

Yeah I don't see how we'd patch that, it's a pretty significant change.

@Eilon
Copy link
Member

Eilon commented Aug 16, 2017

Given that I don't see a reasonable way to patch in 1.x, and there's an easy to use workaround, and that it's fixed in 2.0, I'm closing this issue. Thanks!

@delraydavis
Copy link

unless I am not understanding the issue, I think adding the [Produces("application/json")] decorator to the class should address the problem

# for free to subscribe to this conversation on GitHub. Already have an account? #.
Projects
None yet
Development

No branches or pull requests

6 participants