-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Simple string returned by controller action is not a valid JSON! #4945
Comments
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 BackgroundThe 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
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 So we can update the table from above to describe what usually will happen:
The MistakeThe 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 The FixOur future fix for this will likely be to drop case 2. The
This is nice because you can control the default experience by using 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 WorkaroundMy 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. |
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? |
Workaround should be (as there is no
|
@leppie - thanks, updated my post. |
Do I understand correctly? Does this include a |
No. What I mean by this is that we (by default) ignore the 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 If this doesn't do what you want for some reason you can turn it off.
|
@rynowak ok then! |
Let's take a stab at implementing the solution that @rynowak suggested. |
@kichalla thanks! |
Just bit by this in ASP.NET Core 2.0 when there is no accept header. |
ASP.NET MVC Core 1.0.0 does this:
Meanwhile in ASP.NET Web API 5.2.3 does this:
ASP.NET MVC 5.2.3, when returning
Json("Hello World", JsonRequestBehavior.AllowGet)
, does this:This is an unexpected behavior for me. Is this a bug or feature? I believe
"Hello World!"
is a valid JSON response, notHello World!
. JavascriptJSON.parse()
function can parse the former but not the latter.The text was updated successfully, but these errors were encountered: