/// <inheritdoc /> public bool CanWriteResult(OutputFormatterCanWriteContext context) { // ignore the contentType and just look at the content. // This formatter will be selected if the content is null. // We check for Task as a user can directly create an ObjectContentResult with the unwrapped type. if (context.ObjectType == typeof(void) || context.ObjectType == typeof(Task)) { return(true); } return(TreatNullValueAsNoContent && context.Object == null); }
/// <inheritdoc /> public bool CanWriteResult(OutputFormatterCanWriteContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } // Ignore the passed in content type, if the object is a Stream. if (context.Object is Stream) { return(true); } return(false); }
public override bool CanWriteResult(OutputFormatterCanWriteContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (context.ObjectType == typeof(string) || context.Object is string) { // Call into base to check if the current request's content type is a supported media type. return(base.CanWriteResult(context)); } return(false); }
/// <inheritdoc /> public virtual bool CanWriteResult(OutputFormatterCanWriteContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (SupportedMediaTypes.Count == 0) { var message = Resources.FormatFormatter_NoMediaTypes( GetType().FullName, nameof(SupportedMediaTypes)); throw new InvalidOperationException(message); } if (!CanWriteType(context.ObjectType)) { return(false); } if (!context.ContentType.HasValue) { // If the desired content type is set to null, then the current formatter can write anything // it wants. context.ContentType = new StringSegment(SupportedMediaTypes[0]); return(true); } else { // Confirm this formatter supports a more specific media type than requested e.g. OK if "text/*" // requested and formatter supports "text/plain". contentType is typically what we got in an Accept // header. var parsedContentType = new MediaType(context.ContentType); for (var i = 0; i < SupportedMediaTypes.Count; i++) { var supportedMediaType = new MediaType(SupportedMediaTypes[i]); if (supportedMediaType.IsSubsetOf(parsedContentType)) { context.ContentType = new StringSegment(SupportedMediaTypes[i]); return(true); } } } return(false); }
public override bool CanWriteResult(OutputFormatterCanWriteContext context) { bool result = false; if (this.CanWriteType(context.ObjectType)) { if ((context.ContentType != null) && (context.ContentType.StartsWith("application", StringComparison.InvariantCultureIgnoreCase)) && context.ContentType.EndsWith("+xml", StringComparison.InvariantCultureIgnoreCase)) { result = true; } else { result = base.CanWriteResult(context); } } return(result); }
public override bool CanWriteResult(OutputFormatterCanWriteContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } // Ignore the passed in content type, if the object is string // always return it as a text/plain format. if (context.ObjectType == typeof(string) || context.Object is string) { if (!context.ContentType.HasValue) { var mediaType = SupportedMediaTypes[0]; var encoding = SupportedEncodings[0]; context.ContentType = new StringSegment(MediaType.ReplaceEncoding(mediaType, encoding)); } return(true); } return(false); }
/// <inheritdoc /> public bool CanWriteResult(OutputFormatterCanWriteContext context) { return(context.FailedContentNegotiation ?? false); }
/// <inheritdoc /> public virtual bool CanWriteResult(OutputFormatterCanWriteContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (SupportedMediaTypes.Count == 0) { var message = Resources.FormatFormatter_NoMediaTypes( GetType().FullName, nameof(SupportedMediaTypes)); throw new InvalidOperationException(message); } if (!CanWriteType(context.ObjectType)) { return(false); } if (!context.ContentType.HasValue) { // If the desired content type is set to null, then the current formatter can write anything // it wants. context.ContentType = new StringSegment(SupportedMediaTypes[0]); return(true); } else { var parsedContentType = new MediaType(context.ContentType); for (var i = 0; i < SupportedMediaTypes.Count; i++) { var supportedMediaType = new MediaType(SupportedMediaTypes[i]); if (supportedMediaType.HasWildcard) { // For supported media types that are wildcard patterns, confirm that the requested // media type satisfies the wildcard pattern (e.g., if "text/entity+json;v=2" requested // and formatter supports "text/*+json"). // We only do this when comparing against server-defined content types (e.g., those // from [Produces] or Response.ContentType), otherwise we'd potentially be reflecting // back arbitrary Accept header values. if (context.ContentTypeIsServerDefined && parsedContentType.IsSubsetOf(supportedMediaType)) { return(true); } } else { // For supported media types that are not wildcard patterns, confirm that this formatter // supports a more specific media type than requested e.g. OK if "text/*" requested and // formatter supports "text/plain". // contentType is typically what we got in an Accept header. if (supportedMediaType.IsSubsetOf(parsedContentType)) { context.ContentType = new StringSegment(SupportedMediaTypes[i]); return(true); } } } } return(false); }