GetSupportedContentTypes to take in declared and runtime type.

This commit is contained in:
harshgMSFT 2014-09-02 16:40:40 -07:00
parent 7448bf2843
commit abf27d883f
8 changed files with 17 additions and 25 deletions

View File

@ -94,8 +94,9 @@ namespace Microsoft.AspNet.Mvc
foreach (var formatter in formatters)
{
var supportedContentTypes = formatter.GetSupportedContentTypes(
GetObjectType(formatterContext),
contentType: null);
formatterContext.DeclaredType,
formatterContext.Object?.GetType(),
contentType: null);
if (formatter.CanWriteResult(formatterContext, supportedContentTypes?.FirstOrDefault()))
{
@ -218,18 +219,5 @@ 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

@ -20,12 +20,13 @@ namespace Microsoft.AspNet.Mvc
{
/// <summary>
/// Gets a filtered list of content types which are supported by this formatter
/// for the <paramref name="dataType"/> and <paramref name="contentType"/>.
/// for the <paramref name="declaredType"/> and <paramref name="contentType"/>.
/// </summary>
/// <param name="dataType">Type for which the supported content types are desired.</param>
/// <param name="declaredType">The declared type for which the supported content types are desired.</param>
/// <param name="instanceType">The runtime 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);
/// <returns>Content types which are supported by this formatter.</returns>
IReadOnlyList<MediaTypeHeaderValue> GetSupportedContentTypes(Type declaredType, Type instanceType, MediaTypeHeaderValue contentType);
/// <summary>
/// Determines whether this <see cref="IOutputFormatter"/> can serialize

View File

@ -21,7 +21,7 @@ namespace Microsoft.AspNet.Mvc
return context.Object == null;
}
public IReadOnlyList<MediaTypeHeaderValue> GetSupportedContentTypes(Type dataType, MediaTypeHeaderValue contentType)
public IReadOnlyList<MediaTypeHeaderValue> GetSupportedContentTypes(Type declaredType, Type runtimeType, MediaTypeHeaderValue contentType)
{
return null;
}

View File

@ -40,7 +40,7 @@ namespace Microsoft.AspNet.Mvc
public IList<MediaTypeHeaderValue> SupportedMediaTypes { get; private set; }
/// <inheritdoc />
public virtual IReadOnlyList<MediaTypeHeaderValue> GetSupportedContentTypes(Type dataType, MediaTypeHeaderValue contentType)
public virtual IReadOnlyList<MediaTypeHeaderValue> GetSupportedContentTypes(Type declaredType, Type runtimeType, MediaTypeHeaderValue contentType)
{
var mediaTypes = new List<MediaTypeHeaderValue>();

View File

@ -550,7 +550,8 @@ namespace Microsoft.AspNet.Mvc.Core.Test.ActionResults
return false;
}
public IReadOnlyList<MediaTypeHeaderValue> GetSupportedContentTypes(Type dataType,
public IReadOnlyList<MediaTypeHeaderValue> GetSupportedContentTypes(Type declaredType,
Type runtimeType,
MediaTypeHeaderValue contentType)
{
return null;

View File

@ -118,7 +118,8 @@ namespace Microsoft.AspNet.Mvc
return true;
}
public IReadOnlyList<MediaTypeHeaderValue> GetSupportedContentTypes(Type dataType,
public IReadOnlyList<MediaTypeHeaderValue> GetSupportedContentTypes(Type declaredType,
Type runtimeType,
MediaTypeHeaderValue contentType)
{
return null;

View File

@ -62,7 +62,8 @@ namespace Microsoft.AspNet.Mvc.Core
throw new NotImplementedException();
}
public IReadOnlyList<MediaTypeHeaderValue> GetSupportedContentTypes(Type dataType,
public IReadOnlyList<MediaTypeHeaderValue> GetSupportedContentTypes(Type declaredType,
Type runtimeType,
MediaTypeHeaderValue contentType)
{
return null;

View File

@ -74,7 +74,7 @@ namespace ConnegWebsite
return contentType == null;
}
public IReadOnlyList<MediaTypeHeaderValue> GetSupportedContentTypes(Type dataType, MediaTypeHeaderValue contentType)
public IReadOnlyList<MediaTypeHeaderValue> GetSupportedContentTypes(Type declaredType, Type runtimeType, MediaTypeHeaderValue contentType)
{
return null;
}