Responding to comments

This commit is contained in:
harshgMSFT 2014-08-28 12:53:24 -07:00
parent 33173d3031
commit 7448bf2843
8 changed files with 81 additions and 85 deletions

View File

@ -37,7 +37,7 @@ namespace Microsoft.AspNet.Mvc
{
DeclaredType = DeclaredType,
ActionContext = context,
Object = Value,
Object = Value,
};
var selectedFormatter = SelectFormatter(formatterContext, formatters);
@ -89,12 +89,15 @@ namespace Microsoft.AspNet.Mvc
// This would be the case when no formatter could write the type base on the
// accept headers and the request content type. Fallback on type based match.
if(selectedFormatter == null)
if (selectedFormatter == null)
{
foreach (var formatter in formatters)
{
if (formatter.CanWriteResult(formatterContext,
formatter.SupportedMediaTypes?.FirstOrDefault()))
var supportedContentTypes = formatter.GetSupportedContentTypes(
GetObjectType(formatterContext),
contentType: null);
if (formatter.CanWriteResult(formatterContext, supportedContentTypes?.FirstOrDefault()))
{
return formatter;
}
@ -175,7 +178,7 @@ namespace Microsoft.AspNet.Mvc
IEnumerable<MediaTypeHeaderValue> acceptableContentTypes)
{
var selectedFormatter = formatters.FirstOrDefault(
formatter =>
formatter =>
acceptableContentTypes
.Any(contentType =>
formatter.CanWriteResult(formatterContext, contentType)));
@ -215,5 +218,18 @@ namespace Microsoft.AspNet.Mvc
return formatters;
}
private Type GetObjectType([NotNull] OutputFormatterContext context)
{
if (context.DeclaredType == null || context.DeclaredType == typeof(object))
{
if (context.Object != null)
{
return context.Object.GetType();
}
}
return context.DeclaredType;
}
}
}

View File

@ -19,17 +19,13 @@ namespace Microsoft.AspNet.Mvc
public interface IOutputFormatter
{
/// <summary>
/// Gets the mutable collection of character encodings supported by
/// this <see cref="IOutputFormatter"/>. The encodings are
/// used when writing the data.
/// Gets a filtered list of content types which are supported by this formatter
/// for the <paramref name="dataType"/> and <paramref name="contentType"/>.
/// </summary>
IList<Encoding> SupportedEncodings { get; }
/// <summary>
/// Gets the mutable collection of <see cref="MediaTypeHeaderValue"/> elements supported by
/// this <see cref="IOutputFormatter"/>.
/// </summary>
IList<MediaTypeHeaderValue> SupportedMediaTypes { get; }
/// <param name="dataType">Type for which the supported content types are desired.</param>
/// <param name="contentType">Content type for which the supported content types are desired.</param>
/// <returns>Content types which can are supported by this formatter.</returns>
IReadOnlyList<MediaTypeHeaderValue> GetSupportedContentTypes(Type dataType, MediaTypeHeaderValue contentType);
/// <summary>
/// Determines whether this <see cref="IOutputFormatter"/> can serialize

View File

@ -14,10 +14,6 @@ namespace Microsoft.AspNet.Mvc
/// </summary>
public class NoContentFormatter : IOutputFormatter
{
public IList<Encoding> SupportedEncodings { get; private set; }
public IList<MediaTypeHeaderValue> SupportedMediaTypes { get; private set; }
public bool CanWriteResult(OutputFormatterContext context, MediaTypeHeaderValue contentType)
{
// ignore the contentType and just look at the content.
@ -25,6 +21,11 @@ namespace Microsoft.AspNet.Mvc
return context.Object == null;
}
public IReadOnlyList<MediaTypeHeaderValue> GetSupportedContentTypes(Type dataType, MediaTypeHeaderValue contentType)
{
return null;
}
public Task WriteAsync(OutputFormatterContext context)
{
var response = context.ActionContext.HttpContext.Response;

View File

@ -26,12 +26,36 @@ namespace Microsoft.AspNet.Mvc
SupportedMediaTypes = new List<MediaTypeHeaderValue>();
}
/// <inheritdoc />
/// <summary>
/// Gets the mutable collection of character encodings supported by
/// this <see cref="OutputFormatter"/>. The encodings are
/// used when writing the data.
/// </summary>
public IList<Encoding> SupportedEncodings { get; private set; }
/// <inheritdoc />
/// <summary>
/// Gets the mutable collection of <see cref="MediaTypeHeaderValue"/> elements supported by
/// this <see cref="OutputFormatter"/>.
/// </summary>
public IList<MediaTypeHeaderValue> SupportedMediaTypes { get; private set; }
/// <inheritdoc />
public virtual IReadOnlyList<MediaTypeHeaderValue> GetSupportedContentTypes(Type dataType, MediaTypeHeaderValue contentType)
{
var mediaTypes = new List<MediaTypeHeaderValue>();
if (contentType == null)
{
mediaTypes.AddRange(SupportedMediaTypes);
}
else
{
mediaTypes.Add(SupportedMediaTypes.FirstOrDefault(mt => mt.IsSubsetOf(contentType)));
}
return mediaTypes;
}
/// <summary>
/// Determines the best <see cref="Encoding"/> amongst the supported encodings
/// for reading or writing an HTTP entity body based on the provided <paramref name="contentTypeHeader"/>.

View File

@ -545,27 +545,17 @@ namespace Microsoft.AspNet.Mvc.Core.Test.ActionResults
public class CannotWriteFormatter : IOutputFormatter
{
public IList<Encoding> SupportedEncodings
{
get
{
return null;
}
}
public IList<MediaTypeHeaderValue> SupportedMediaTypes
{
get
{
return null;
}
}
public virtual bool CanWriteResult(OutputFormatterContext context, MediaTypeHeaderValue contentType)
{
return false;
}
public IReadOnlyList<MediaTypeHeaderValue> GetSupportedContentTypes(Type dataType,
MediaTypeHeaderValue contentType)
{
return null;
}
public virtual Task WriteAsync(OutputFormatterContext context)
{
throw new NotImplementedException();

View File

@ -113,27 +113,17 @@ namespace Microsoft.AspNet.Mvc
{
public Encoding Encoding { get; set; }
public IList<Encoding> SupportedEncodings
{
get
{
return null;
}
}
public IList<MediaTypeHeaderValue> SupportedMediaTypes
{
get
{
return null;
}
}
public bool CanWriteResult(OutputFormatterContext context, MediaTypeHeaderValue contentType)
{
return true;
}
public IReadOnlyList<MediaTypeHeaderValue> GetSupportedContentTypes(Type dataType,
MediaTypeHeaderValue contentType)
{
return null;
}
public async Task WriteAsync(OutputFormatterContext context)
{
// Override using the selected encoding.

View File

@ -57,27 +57,17 @@ namespace Microsoft.AspNet.Mvc.Core
private class TestOutputFormatter : IOutputFormatter
{
public IList<Encoding> SupportedEncodings
{
get
{
return null;
}
}
public IList<MediaTypeHeaderValue> SupportedMediaTypes
{
get
{
return null;
}
}
public bool CanWriteResult(OutputFormatterContext context, MediaTypeHeaderValue contentType)
{
throw new NotImplementedException();
}
public IReadOnlyList<MediaTypeHeaderValue> GetSupportedContentTypes(Type dataType,
MediaTypeHeaderValue contentType)
{
return null;
}
public Task WriteAsync(OutputFormatterContext context)
{
throw new NotImplementedException();

View File

@ -68,28 +68,17 @@ namespace ConnegWebsite
public class StopIfNoMatchOutputFormatter : IOutputFormatter
{
public IList<Encoding> SupportedEncodings
{
get
{
return null;
}
}
public IList<MediaTypeHeaderValue> SupportedMediaTypes
{
get
{
return null;
}
}
// Select if no Registered content type.
public bool CanWriteResult(OutputFormatterContext context, MediaTypeHeaderValue contentType)
{
return contentType == null;
}
public IReadOnlyList<MediaTypeHeaderValue> GetSupportedContentTypes(Type dataType, MediaTypeHeaderValue contentType)
{
return null;
}
public Task WriteAsync(OutputFormatterContext context)
{
var response = context.ActionContext.HttpContext.Response;