When a Controller operation returns byte[]
the content should automatically be set to application/octet-stream
#7926
Comments
There is a set of |
Why doesn't returning |
The Json \ Xml formatters convert it to a base-64 encoded string. So it does work today, just not what @nockawa is expecting it to do. |
That's interesting, because returning a Stream is treated differently to returning a |
I'd suggest implementing something like this with a formatter. If you want to send a PR, we can put something like this in the box for others to use (off by default). I don't think there's much reason why we would consider changing the defaults, as mentioned here, we already have a defined behavior here that most users will see (JSON + Base64 encoding) |
Right, this would be a breaking change otherwise. |
@pranavkm @rynowak Tell me if I understand this correctly: Other questions:
Thanks, |
Formatters ( Here's an example of a simple one: https://github.com/aspnet/Mvc/blob/dev/src/Microsoft.AspNetCore.Mvc.Core/Formatters/StreamOutputFormatter.cs The answers to your questions:
|
Sorry, but I still don't understand why when I set the content type to |
In general, MVC delegates how .NET objects are formatted into responses through Output formatters. |
@mkArtakMSFT @rynowak I think @nockawa is right here. It actually wouldn't be a breaking change to introduce this behavior and it would meet customer expectations. Basically we would have a BinaryOutputFormatter that could handle the |
I agree that it's not a breaking change - if it's limited to cases where conneg decides that For context, I'm officially on the record as opposed to putting in more on by default output formatters for specialized scenarios. I was also against What would change my mind about this is understanding how this is much better than both what we already have ( Would we tell everyone to 'do it this way'? Would we change our documentation to say that this is the correct way to return bytes vs Keep in mind we're talking about the case where user code sets both the content type and returns bytes 😆 - and we're talking about it in the context of adding a new thing to an existing set of features - not the world of green-field, ideal design. We haven't yet built any features that are targeted only at cases where application code sets a content type. So your code either looks like:
OR
OR
|
In my case I'm looking for the fastest way for my WebApi method to operate. I'm returning a raw bytes of a serialized Protobuf object that is being cached to avoid multiple serializations. (which has nothing to do with File...) So the object I have on the server site in a But for the unadvised user (and sorry, but considering the state of the documentation so far, it will happen), to set the content type and returning a I don't understand why such natural behavior is the center such debate:
I've convinced people in my company to switch to .net core because asp.net core is fast and simple to code...The web is not all about returning json data... |
Is this a Bug or Feature request?:
From my standpoint it's a bug, a design one at least, but I could understand it perceived as a feature.
Steps to reproduce (preferably a link to a GitHub repo with a repro project):
This post from @davidfowl demonstrates that returning a
byte[]
doesn't work as one should expect: setting automatically the content asapplication/octet-stream
and having MVC returning the data properly.From the example below, the first method doesn't work and the two next ones do.
Description of the problem:
When one wants to return binary data in the HTTP body of the Response, right now the only way is to rely on Stream, returning
byte[]
won't work but it is however a very natural way to do it imho. The content type should be automatically set asbyte[]
could only be perceived as binary data.Right now while returning this type the requester always gets a StatusCode of 406 (not acceptable) and no content.
Version of
Microsoft.AspNetCore.Mvc
:2.1
The text was updated successfully, but these errors were encountered: