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

@ -89,12 +89,15 @@ namespace Microsoft.AspNet.Mvc
// This would be the case when no formatter could write the type base on the // 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. // accept headers and the request content type. Fallback on type based match.
if(selectedFormatter == null) if (selectedFormatter == null)
{ {
foreach (var formatter in formatters) foreach (var formatter in formatters)
{ {
if (formatter.CanWriteResult(formatterContext, var supportedContentTypes = formatter.GetSupportedContentTypes(
formatter.SupportedMediaTypes?.FirstOrDefault())) GetObjectType(formatterContext),
contentType: null);
if (formatter.CanWriteResult(formatterContext, supportedContentTypes?.FirstOrDefault()))
{ {
return formatter; return formatter;
} }
@ -215,5 +218,18 @@ namespace Microsoft.AspNet.Mvc
return formatters; 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 public interface IOutputFormatter
{ {
/// <summary> /// <summary>
/// Gets the mutable collection of character encodings supported by /// Gets a filtered list of content types which are supported by this formatter
/// this <see cref="IOutputFormatter"/>. The encodings are /// for the <paramref name="dataType"/> and <paramref name="contentType"/>.
/// used when writing the data.
/// </summary> /// </summary>
IList<Encoding> SupportedEncodings { 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>
/// <summary> /// <returns>Content types which can are supported by this formatter.</returns>
/// Gets the mutable collection of <see cref="MediaTypeHeaderValue"/> elements supported by IReadOnlyList<MediaTypeHeaderValue> GetSupportedContentTypes(Type dataType, MediaTypeHeaderValue contentType);
/// this <see cref="IOutputFormatter"/>.
/// </summary>
IList<MediaTypeHeaderValue> SupportedMediaTypes { get; }
/// <summary> /// <summary>
/// Determines whether this <see cref="IOutputFormatter"/> can serialize /// Determines whether this <see cref="IOutputFormatter"/> can serialize

View File

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

View File

@ -26,12 +26,36 @@ namespace Microsoft.AspNet.Mvc
SupportedMediaTypes = new List<MediaTypeHeaderValue>(); 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; } 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; } 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> /// <summary>
/// Determines the best <see cref="Encoding"/> amongst the supported encodings /// 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"/>. /// 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 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) public virtual bool CanWriteResult(OutputFormatterContext context, MediaTypeHeaderValue contentType)
{ {
return false; return false;
} }
public IReadOnlyList<MediaTypeHeaderValue> GetSupportedContentTypes(Type dataType,
MediaTypeHeaderValue contentType)
{
return null;
}
public virtual Task WriteAsync(OutputFormatterContext context) public virtual Task WriteAsync(OutputFormatterContext context)
{ {
throw new NotImplementedException(); throw new NotImplementedException();

View File

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

View File

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

View File

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