示例#1
0
        public override Task <object> ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable <MediaTypeFormatter> formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)
        {
            /*********************** 代码块解释 *********************************
             * 以下代码大部分拷贝自 WebApi 中基类的源码。
             * 中间要完成的工作主要是:当路由中提供了 id,那么应该把这个值存储下来,并替换 HttpContent。
             * 注意:
             * 新建了一个类型 IdFromRouteHttpContent 用于把 Id 的值存储下来。
             **********************************************************************/

            HttpContent content = request.Content;

            if (content != null)
            {
                Task <object> result;
                try
                {
                    /*********************** 代码块解释 *********************************
                     * //modified ------------start;*/
                    //如果路由中提供了 id,那么应该把这个值存储下来。
                    var    route = this.Descriptor.Configuration.Routes.GetRouteData(request);
                    object id    = null;
                    if (route.Values.TryGetValue("id", out id) && id != null && id != RouteParameter.Optional)
                    {
                        var idContent = new IdFromRouteHttpContent(content, id.ToString());
                        result = idContent.ReadAsAsync(type, formatters, formatterLogger, cancellationToken);
                    }
                    else
                    {
                        result = content.ReadAsAsync(type, formatters, formatterLogger, cancellationToken);
                    }

                    /*modified------------end.
                     * **********************************************************************/
                }
                catch (UnsupportedMediaTypeException ex)
                {
                    string format = (content.Headers.ContentType == null) ? "SRResources.UnsupportedMediaTypeNoContentType" : "SRResources.UnsupportedMediaType";
                    throw new HttpResponseException(request.CreateErrorResponse(HttpStatusCode.UnsupportedMediaType, string.Format(format, new object[]
                    {
                        ex.MediaType.MediaType
                    }), ex));
                }
                return(result);
            }
            object defaultValueForType = MediaTypeFormatter.GetDefaultValueForType(type);

            if (defaultValueForType == null)
            {
                return(Task.FromResult <object>(null));
            }
            return(Task.FromResult(defaultValueForType));
        }
        public override Task<object> ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable<MediaTypeFormatter> formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)
        {
            /*********************** 代码块解释 *********************************
             * 以下代码大部分拷贝自 WebApi 中基类的源码。
             * 中间要完成的工作主要是:当路由中提供了 id,那么应该把这个值存储下来,并替换 HttpContent。
             * 注意:
             * 新建了一个类型 IdFromRouteHttpContent 用于把 Id 的值存储下来。
            **********************************************************************/

            HttpContent content = request.Content;

            if (content != null)
            {
                Task<object> result;
                try
                {
                    /*********************** 代码块解释 *********************************
                    //modified ------------start;*/
                    //如果路由中提供了 id,那么应该把这个值存储下来。
                    var route = this.Descriptor.Configuration.Routes.GetRouteData(request);
                    object id = null;
                    if (route.Values.TryGetValue("id", out id) && id != null && id != RouteParameter.Optional)
                    {
                        var idContent = new IdFromRouteHttpContent(content, id.ToString());
                        result = idContent.ReadAsAsync(type, formatters, formatterLogger, cancellationToken);
                    }
                    else
                    {
                        result = content.ReadAsAsync(type, formatters, formatterLogger, cancellationToken);
                    }
                    /*modified------------end.
                     * **********************************************************************/
                }
                catch (UnsupportedMediaTypeException ex)
                {
                    string format = (content.Headers.ContentType == null) ? "SRResources.UnsupportedMediaTypeNoContentType" : "SRResources.UnsupportedMediaType";
                    throw new HttpResponseException(request.CreateErrorResponse(HttpStatusCode.UnsupportedMediaType, string.Format(format, new object[]
                    {
                        ex.MediaType.MediaType
                    }), ex));
                }
                return result;
            }
            object defaultValueForType = MediaTypeFormatter.GetDefaultValueForType(type);
            if (defaultValueForType == null)
            {
                return Task.FromResult<object>(null);
            }
            return Task.FromResult(defaultValueForType);
        }