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
By default, the runtime handles requests without an explicit Accept header by returning a JSON formatted response. However, it seems that after configuring a custom output formatter, it replaces this default so that every request that doesn't specify a format uses the custom format.
Moreover, it appears that the runtime chooses the formatter based on its data type instead of the supported media types that the formatter declares. For example, if the custom formatter derives from JsonOutputFormatter, it also replaces the standard JSON formatter for any requests that explicitly specify a JSON response, even if the supported media types of the custom formatter do not include "application/json".
Neither of the two behaviors described above match the current behavior in ASP.NET Core applications.
For example, the code below registers a formatter for the "application/xxx" media type. After including this code, any requests that do not explicitly request a format are handled by the custom formatter.
publicclassMyCustomOutputFormatter:TextOutputFormatter{publicMyCustomOutputFormatter(){this.SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse("application/xxx"));this.SupportedEncodings.Add(Encoding.UTF8);this.SupportedEncodings.Add(Encoding.Unicode);}publicoverrideasync Task WriteResponseBodyAsync(OutputFormatterWriteContextcontext,EncodingselectedEncoding){await context.HttpContext.Response.WriteAsync("This is my custom formatter");}}publicclassStartup:FunctionsStartup{publicoverridevoidConfigure(IFunctionsHostBuilderbuilder){
services.AddMvcCore(config =>{ config.OutputFormatters.Add(new MyCustomOutputFormatter());});}}
In fact, when multiple custom formatters are added, the default formatter becomes the first one that was registered. So a workaround seems to be to register JsonOutputFormatter as the first one, though I'm not sure whether that could result in problems elsewhere given that, presumably, the runtime will also register it.
When the custom formatter is changed to derive from JsonOutputFormatter instead of TextOutputFormatter, as shown below, it will start handling all JSON requests despite it clearing the SupportedMediaTypes list and specifying "application/xxx" only.
publicclassMyCustomJsonOutputFormatter:JsonOutputFormatter{publicMyCustomJsonOutputFormatter(JsonSerializerSettingssettings):base(new JsonSerializerSettings(),ArrayPool<char>.Create()){this.SupportedMediaTypes.Clear();this.SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse("application/xxx"));}publicoverride Task WriteResponseBodyAsync(OutputFormatterWriteContextcontext,EncodingselectedEncoding){varresponse= context.HttpContext.Response;varbuffer= Encoding.UTF8.GetBytes("This is a JSON derived custom formatter");
response.Body.WriteAsync(buffer,0, buffer.Length);}}
The text was updated successfully, but these errors were encountered:
Hi @f2bo , Apologies for the delayed response. Were you able to find a solution here. Kindly let us know if you are you still facing this challenge? If yes, then please provide us repro steps so as to clone the issue.
This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.
By default, the runtime handles requests without an explicit
Accept
header by returning a JSON formatted response. However, it seems that after configuring a custom output formatter, it replaces this default so that every request that doesn't specify a format uses the custom format.Moreover, it appears that the runtime chooses the formatter based on its data type instead of the supported media types that the formatter declares. For example, if the custom formatter derives from
JsonOutputFormatter
, it also replaces the standard JSON formatter for any requests that explicitly specify a JSON response, even if the supported media types of the custom formatter do not include "application/json".Neither of the two behaviors described above match the current behavior in ASP.NET Core applications.
For example, the code below registers a formatter for the "application/xxx" media type. After including this code, any requests that do not explicitly request a format are handled by the custom formatter.
In fact, when multiple custom formatters are added, the default formatter becomes the first one that was registered. So a workaround seems to be to register
JsonOutputFormatter
as the first one, though I'm not sure whether that could result in problems elsewhere given that, presumably, the runtime will also register it.When the custom formatter is changed to derive from
JsonOutputFormatter
instead ofTextOutputFormatter
, as shown below, it will start handling all JSON requests despite it clearing theSupportedMediaTypes
list and specifying "application/xxx" only.The text was updated successfully, but these errors were encountered: