You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Dec 14, 2018. It is now read-only.
Repository navigation
This repository was archived by the owner on Dec 14, 2018. It is now read-only.
Simple string returned by controller action is not a valid JSON! #4945
This is an unexpected behavior for me. Is this a bug or feature? I believe "Hello World!" is a valid JSON response, not Hello World!. Javascript JSON.parse() function can parse the former but not the latter.
Reacted by Nikolay Kostov, Llewellyn Pritchard, jods and Michael Pavlak
I had a chance to discuss this with @Eilon this AM, and I think that while the current behavior is what was intended, it's not super useful. It's also not something we'd be willing to change for a 1.1.0 - hence the 2.0.0 label.
Some Background
The intention here was to make it easy and possible to return plaintext from an action, the "Hello, World" scenario. This has long been MVC's behavior - but, as you pointed out this conflicts with WebAPI's historical behavior which would be to content-negotiate and choose XML or JSON based on your configuration.
The compromise for this was to introduce StringOutputFormatter - a formatter that is only active for string objects and will do the following:
If there is no 'best' content type then return the text verbatim with text/plain
If there is a 'best' content type then return the text verbatim with the 'best' content type
I'm glossing over a bit what we mean exactly by 'best' content type - what you should know is that 'best' typically means what's in the accept header or [Produces(...)]OR most importantly if your request is coming from a browser, the accept header is ignored and there is no 'best' unless you are using [Produces(...)].
So we can update the table from above to describe what usually will happen:
If the request comes from a browser, then return the text verbatim with text/plain
If the request came from an API client, there is a 'best' content type and so return the text verbatim with the 'best' content type
The Mistake
The mistake here is in this case:
If the request came from an API client, there is a 'best' content type and so return the text verbatim with the 'best' content type
The assumptions here on our part are wrong. You can't write an action that returns text that's meant to be written verbatim in multiple formats. If you had done that, it's because you did conneg already in the action, and don't need us to do it for you. If you manually formatted a string as JSON in your action and returned it, we wouldn't be able to somehow make it XML (or any other format).
So clearly this behavior isn't useful, and it's what you're running into as there's a Content-Type: application/json; charset=utf-8 in the response.
The Fix
Our future fix for this will likely be to drop case 2. The StringOutputFormatter will behave like this:
If the request comes from a browser, then return the text verbatim with text/plain
If the request came from an API client requesting text/plain return the text verbatim
This is nice because you can control the default experience by using [Produces(...)] as a global filter.
Unfortunately, this is subtle change in behavior that we're not comfortable making in a 1.1.0 release. Someone out there has probably taken a dependency on this even though it's not optimal.
The Workaround
My suggestion if you don't like how this works today is just to remove the string formatter.
Do I understand correctly? Does this include a Fetch request from a JS application? If the answer is yes, I don't understand why you ignore the client Accept header. If my app asks for application/json you SHOULD return that and not text/plain.
Does this include a Fetch request from a JS application?
No.
What I mean by this is that we (by default) ignore the Accept header when it contains the the wildcard */*. This generally means that the client is something that's going to just display the text verbatim rather than trying to understand the data. In that case we want the ordering of formatters to take over rather than relying on what's in the Accept header.
We'd ignore these, give each formatter in sequence a chance to respond. API clients tend to be specific about what they want and won't claim to Accept: */*.
If this doesn't do what you want for some reason you can turn it off.
Activity
rynowak commentedon Jul 5, 2016
I had a chance to discuss this with @Eilon this AM, and I think that while the current behavior is what was intended, it's not super useful. It's also not something we'd be willing to change for a 1.1.0 - hence the 2.0.0 label.
Some Background
The intention here was to make it easy and possible to return plaintext from an action, the "Hello, World" scenario. This has long been MVC's behavior - but, as you pointed out this conflicts with WebAPI's historical behavior which would be to content-negotiate and choose XML or JSON based on your configuration.
The compromise for this was to introduce
StringOutputFormatter- a formatter that is only active forstringobjects and will do the following:text/plainI'm glossing over a bit what we mean exactly by 'best' content type - what you should know is that 'best' typically means what's in the accept header or
[Produces(...)]OR most importantly if your request is coming from a browser, the accept header is ignored and there is no 'best' unless you are using[Produces(...)].So we can update the table from above to describe what usually will happen:
text/plainThe Mistake
The mistake here is in this case:
The assumptions here on our part are wrong. You can't write an action that returns text that's meant to be written verbatim in multiple formats. If you had done that, it's because you did conneg already in the action, and don't need us to do it for you. If you manually formatted a string as JSON in your action and returned it, we wouldn't be able to somehow make it XML (or any other format).
So clearly this behavior isn't useful, and it's what you're running into as there's a
Content-Type: application/json; charset=utf-8in the response.The Fix
Our future fix for this will likely be to drop case 2. The
StringOutputFormatterwill behave like this:text/plaintext/plainreturn the text verbatimThis is nice because you can control the default experience by using
[Produces(...)]as a global filter.Unfortunately, this is subtle change in behavior that we're not comfortable making in a 1.1.0 release. Someone out there has probably taken a dependency on this even though it's not optimal.
The Workaround
My suggestion if you don't like how this works today is just to remove the string formatter.
If you're writing an API, it's not likely that this would enable anything you need anyway.
ryanelian commentedon Jul 6, 2016
Actually, I find the Core MVC 1.0.0 behavior to be useful because it lets my client consume the text immediately without deserializing.
But I was surprised when I discovered this 'feature' when I was coaching about MVC / EF Core. I thought it was a bug, hence the issue.
Thanks for the heads-up and the workaround. :D
EDIT:
Just tested. Curiously this 'bug' doesn't break AngularJS 1.5.7 $http. Yay?
leppie commentedon Jul 11, 2016
Workaround should be (as there is no
Formattersproperty):rynowak commentedon Jul 11, 2016
@leppie - thanks, updated my post.
jods4 commentedon Jul 20, 2016
@rynowak
Do I understand correctly? Does this include a
Fetchrequest from a JS application? If the answer is yes, I don't understand why you ignore the clientAcceptheader. If my app asks forapplication/jsonyou SHOULD return that and nottext/plain.rynowak commentedon Jul 20, 2016
No.
What I mean by this is that we (by default) ignore the
Acceptheader when it contains the the wildcard*/*. This generally means that the client is something that's going to just display the text verbatim rather than trying to understand the data. In that case we want the ordering of formatters to take over rather than relying on what's in theAcceptheader.For instance, consider headers like this:
We'd ignore these, give each formatter in sequence a chance to respond. API clients tend to be specific about what they want and won't claim to
Accept: */*.If this doesn't do what you want for some reason you can turn it off.
jods4 commentedon Jul 20, 2016
@rynowak ok then!
Eilon commentedon Jan 5, 2017
Let's take a stab at implementing the solution that @rynowak suggested.
[Fixes #4945] Simple string returned by controller action is not a va…
[Fixes #4945] Simple string returned by controller action is not a va…
[Fixes #4945] Simple string returned by controller action is not a va…
[Fixes #4945] Simple string returned by controller action is not a va…
[Fixes #4945] Simple string returned by controller action is not a va…
leppie commentedon Jan 25, 2017
@kichalla thanks!
TylerBrinkley commentedon Apr 26, 2018
Just bit by this in ASP.NET Core 2.0 when there is no accept header.