Exemplo n.º 1
0
        /// <inheritdoc/>
        public override bool CanWriteType(Type type)
        {
            if (type == null)
            {
                throw Error.ArgumentNull("type");
            }

            if (Request != null)
            {
                ODataSerializerProvider serializerProvider = Request.GetRequestContainer()
                                                             .GetRequiredService <ODataSerializerProvider>();

                // See if this type is a SingleResult or is derived from SingleResult.
                bool isSingleResult = false;
                if (type.IsGenericType)
                {
                    Type genericType = type.GetGenericTypeDefinition();
                    Type baseType    = TypeHelper.GetBaseType(type);
                    isSingleResult = (genericType == typeof(SingleResult <>) || baseType == typeof(SingleResult));
                }

                return(ODataOutputFormatterHelper.CanWriteType(
                           type,
                           _payloadKinds,
                           isSingleResult,
                           new WebApiRequestMessage(Request),
                           (objectType) => serializerProvider.GetODataPayloadSerializer(objectType, Request)));
            }

            return(false);
        }
Exemplo n.º 2
0
        /// <inheritdoc/>
        public override bool CanWriteResult(OutputFormatterCanWriteContext context)
        {
            if (context == null)
            {
                throw Error.ArgumentNull("context");
            }

            // Ensure we have a valid request.
            HttpRequest request = context.HttpContext.Request;

            if (request == null)
            {
                throw Error.InvalidOperation(SRResources.ReadFromStreamAsyncMustHaveRequest);
            }

            // Ignore non-OData requests.
            if (request.ODataFeature().Path == null)
            {
                return(false);
            }

            // The following base.CanWriteResult(context) will change the context.ContentType
            // If this formatter can't write the result, we should reset the context.ContentType to its original value.
            // So that, the other formatter can make a descison based on the original content type.
            // Be noted: in .NET 5, the context.ContentType is a new StringSegment everytime when goes into each formatter
            // formatterContext.ContentType = new StringSegment();
            // So, in .NET 5, we don't need to reset the contentType to backupContentType.
            StringSegment backupContentType = context.ContentType;

            // Allow the base class to make its determination, which includes
            // checks for SupportedMediaTypes.
            bool suportedMediaTypeFound = false;

            if (SupportedMediaTypes.Any())
            {
                suportedMediaTypeFound = base.CanWriteResult(context);
            }

            // See if the request satisfies any mappings.
            IEnumerable <MediaTypeMapping> matchedMappings = (MediaTypeMappings == null) ? null : MediaTypeMappings
                                                             .Where(m => (m.TryMatchMediaType(request) > 0));

            // Now pick the best content type. If a media mapping was found, use that and override the
            // value specified by the controller, if any. Otherwise, let the base class decide.
            if (matchedMappings != null && matchedMappings.Any())
            {
                context.ContentType = matchedMappings.First().MediaType.ToString();
            }
            else if (!suportedMediaTypeFound)
            {
                context.ContentType = backupContentType;
                return(false);
            }

            // We need the type in order to write it.
            Type type = context.ObjectType ?? context.Object?.GetType();

            if (type == null)
            {
                context.ContentType = backupContentType;
                return(false);
            }
            type = TypeHelper.GetTaskInnerTypeOrSelf(type);

            ODataSerializerProvider serializerProvider = request.GetRequestContainer().GetRequiredService <ODataSerializerProvider>();

            // See if this type is a SingleResult or is derived from SingleResult.
            bool isSingleResult = false;

            if (type.IsGenericType)
            {
                Type genericType = type.GetGenericTypeDefinition();
                Type baseType    = TypeHelper.GetBaseType(type);
                isSingleResult = (genericType == typeof(SingleResult <>) || baseType == typeof(SingleResult));
            }

            bool result = ODataOutputFormatterHelper.CanWriteType(
                type,
                _payloadKinds,
                isSingleResult,
                new WebApiRequestMessage(request),
                (objectType) => serializerProvider.GetODataPayloadSerializer(objectType, request));

            if (!result)
            {
                context.ContentType = backupContentType;
            }

            return(result);
        }