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

Return ObjectResult - Without Double Quotes #4146

Closed
Fosol opened this issue Feb 24, 2016 · 9 comments
Closed

Return ObjectResult - Without Double Quotes #4146

Fosol opened this issue Feb 24, 2016 · 9 comments
Labels

Comments

@Fosol
Copy link

Fosol commented Feb 24, 2016

Is there such thing as a RawResult? ObjectResult has some annoying behaviour that if you submit a string as the value it will always include double quotes in the response regardless of the ContentType you specify.

I want to ensure no double quotes are included in the response.

@kichalla
Copy link
Member

I suppose the response is being sent back as json, hence the double quotes. This sounds like a bug. Meanwhile you can return a ContentResult which sends content as it is with text/plain as the content type.

@Fosol
Copy link
Author

Fosol commented Feb 24, 2016

I actually need a dynamic content type based on a number of conditions.

@kichalla
Copy link
Member

I see. You could add the StringOutputFormatter in the output formatter list. This formatter writes content in text/plain formatter. Since order of formatter matters, make sure to add it before other formatters like json or xml.

@rynowak
Copy link
Member

rynowak commented Feb 24, 2016

This sounds like a bug.

This is not a bug. This comes up from time to time and the recommendation is to use the string output formatter if you want strings to go onto the wire as-is.

This formatter writes content in text/plain formatter

This changed for RC2. If the returned object is a string, it can write any content type.

I actually need a dynamic content type based on a number of conditions.

If you can give us more information about what you're trying to do we can help.

@Fosol
Copy link
Author

Fosol commented Feb 24, 2016

Sounds like RC2 has a way to return a ContentResult and make sure it doesn't include double quotes around string values, with the additional control of setting the content type?

Let me know if I understand that correctly.

Which means I'll have to wait to upgrade since I'm buried in building in RC1.

@rynowak
Copy link
Member

rynowak commented Feb 24, 2016

Sounds like RC2 has a way to return a ContentResult and make sure it doesn't include double quotes around string values, with the additional control of setting the content type?

This has been around forever: https://github.com/aspnet/Mvc/blob/master/src/Microsoft.AspNet.Mvc.ViewFeatures/Controller.cs#L471

ContentResult will just write out your text verbatim.

@jods4
Copy link

jods4 commented Jun 24, 2016

@rynowak I think you should reconsider this.
I was bitten by it today in RC2.
StringOutputFormatter writes returned string verbatim to the output.
But this makes no sense:

**Request**
Accept: application/json

**Response
Content-Type: application/json

Hello World <-- this is no JSON

As you can see, the server does advertise that it is serializing JSON in Content-Type but the response body is not JSON. Also, the client requested JSON exclusively but gets a 200 OK with no JSON content.
This can only be seen as a bug as the response as a whole is clearly invalid.

I think moving the StringOutputFormatter after JsonOutputFormatter would be reasonable and fix this.
For the use case where I want to return manually serialized JSON verbatim from my Action, ContentResult is there for that and indicate intent. Returning a string is too ambiguous to override the proper content type serialization.

@rynowak rynowak reopened this Jun 24, 2016
@rynowak
Copy link
Member

rynowak commented Jun 24, 2016

I agree, this doesn't seem right or useful.

@rynowak
Copy link
Member

rynowak commented Jul 20, 2016

@jods, see #4945 (comment)

We're planning to change this behavior for 2.0.

I'm going to close this issue because I've already got a long-winded explanation including our future plans in #4945

@rynowak rynowak closed this as completed Jul 20, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants