当前位置: 首页>>代码示例>>C#>>正文


C# HttpResponseMessage.TryGetContentValue方法代码示例

本文整理汇总了C#中System.Net.Http.HttpResponseMessage.TryGetContentValue方法的典型用法代码示例。如果您正苦于以下问题:C# HttpResponseMessage.TryGetContentValue方法的具体用法?C# HttpResponseMessage.TryGetContentValue怎么用?C# HttpResponseMessage.TryGetContentValue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Net.Http.HttpResponseMessage的用法示例。


在下文中一共展示了HttpResponseMessage.TryGetContentValue方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Enrich

        public HttpResponseMessage Enrich(HttpResponseMessage response)
        {
            PostModel post;
            UrlHelper urlHelper = response.RequestMessage.GetUrlHelper();

            if (response.TryGetContentValue<PostModel>(out post))
            {
                Enrich(post, urlHelper);
                return response;
            }

            PostFeed feed;
            if (response.TryGetContentValue<PostFeed>(out feed))
            {
                foreach (var p in feed.Posts)
                {
                    Enrich(p, urlHelper);
                }

                string selfUrl = urlHelper.Link("DefaultApi", new { controller = "posts" });
                feed.AddLink(new SelfLink(selfUrl));
            }

            return response;
        }
开发者ID:prashantkhandelwal,项目名称:Bloggy,代码行数:25,代码来源:PostResponseEnricher.cs

示例2: BuildApiResponse

        private static HttpResponseMessage BuildApiResponse(HttpRequestMessage request, HttpResponseMessage response)
        {
            object content;
            string errorMessage = null;

            if (response.TryGetContentValue(out content) && !response.IsSuccessStatusCode)
            {
                HttpError error = content as HttpError;

                if (error != null)
                {
                    content = null;
                    errorMessage = error.Message;

#if DEBUG
                    errorMessage = string.Concat(errorMessage, error.ExceptionMessage, error.StackTrace);
#endif
                }
            }

            var newResponse = request.CreateResponse(response.StatusCode, new ApiResponse(response.StatusCode, content, errorMessage));

            foreach (var header in response.Headers)
            {
                newResponse.Headers.Add(header.Key, header.Value);
            }

            return newResponse;
        }
开发者ID:swenmeeuwes,项目名称:Project-5-Dataflow-ASP.NET-Webservice,代码行数:29,代码来源:WrappingHandler.cs

示例3: GetResponseMessage

        private HttpResponseMessage GetResponseMessage(HttpRequestMessage request, HttpResponseMessage response)
        {
            object content;

            if (response.TryGetContentValue(out content))
            {
                var error = content as HttpError;

                if (content is string && !response.IsSuccessStatusCode)
                {
                    content = new {error = content};
                }
                else if (error != null)
                {
                    if (response.StatusCode == HttpStatusCode.InternalServerError)
                    {
                        error.Message = "Internal server error.";
                    }
                    content = GetErrorResult(error);
                }
                else
                {
                    content = new {result = content};
                }
            }

            if (content != null)
                response.Content = new ObjectContent(content.GetType(), content, JsonFormatter);
            return response;
        }
开发者ID:Bringan,项目名称:RedditRankedFlairs,代码行数:30,代码来源:ApiMessageHandler.cs

示例4: Enrich

        public HttpResponseMessage Enrich(HttpResponseMessage response) {

            Car car;
            UrlHelper urlHelper = response.RequestMessage.GetUrlHelper();
            if (response.TryGetContentValue<Car>(out car)) {
                Enrich(car, urlHelper);
            }

            IEnumerable<Car> cars;
            if (response.TryGetContentValue<IEnumerable<Car>>(out cars)) {
                foreach (var carItem in cars) {
                    Enrich(carItem, urlHelper);
                }
            }

            return response;
        }
开发者ID:mahf,项目名称:ASPNETWebAPISamples,代码行数:17,代码来源:CarResponseEnricher.cs

示例5: Enrich

        public HttpResponseMessage Enrich(HttpResponseMessage response)
        {
            TodoRepresentation todo;

            var urlHelper = response.RequestMessage.GetUrlHelper();

            if (response.TryGetContentValue(out todo))
            {
                Enrich(todo, urlHelper);
                return response;
            }

            List<TodoRepresentation> representations;
            if (response.TryGetContentValue(out representations))
            {
                representations.ToList().ForEach(p => Enrich(p, urlHelper));
            }

            return response;
        }
开发者ID:chrismcbride76,项目名称:TodoApp-WebApi,代码行数:20,代码来源:TodoResponseEnricher.cs

示例6: Enrich

        public HttpResponseMessage Enrich(HttpResponseMessage response) {

            MediaModel media;

            var urlHelper = response.RequestMessage.GetUrlHelper();

            if (response.TryGetContentValue<MediaModel>(out media)) {
                Enrich(media, urlHelper);
            }

            return response;
        }
开发者ID:mahf,项目名称:ASPNETWebAPISamples,代码行数:12,代码来源:MediaResponseEnricher.cs

示例7: WrapResultIfNeeded

        protected virtual void WrapResultIfNeeded(HttpRequestMessage request, HttpResponseMessage response)
        {
            if (!response.IsSuccessStatusCode)
            {
                return;
            }

            if (_configuration.SetNoCacheForAllResponses)
            {
                //Based on http://stackoverflow.com/questions/49547/making-sure-a-web-page-is-not-cached-across-all-browsers
                response.Headers.CacheControl = new CacheControlHeaderValue
                {
                    NoCache = true,
                    NoStore = true,
                    MaxAge = TimeSpan.Zero,
                    MustRevalidate = true
                };
            }

            var wrapAttr = HttpActionDescriptorHelper.GetWrapResultAttributeOrNull(request.GetActionDescriptor())
                           ?? _configuration.DefaultWrapResultAttribute;

            if (!wrapAttr.WrapOnSuccess)
            {
                return;
            }

            if (IsIgnoredUrl(request.RequestUri))
            {
                return;
            }

            object resultObject;
            if (!response.TryGetContentValue(out resultObject) || resultObject == null)
            {
                response.StatusCode = HttpStatusCode.OK;
                response.Content = new ObjectContent<AjaxResponse>(
                    new AjaxResponse(),
                    _configuration.HttpConfiguration.Formatters.JsonFormatter
                    );
                return;
            }

            if (resultObject is AjaxResponseBase)
            {
                return;
            }

            response.Content = new ObjectContent<AjaxResponse>(
                new AjaxResponse(resultObject),
                _configuration.HttpConfiguration.Formatters.JsonFormatter
                );
        }
开发者ID:yuzukwok,项目名称:aspnetboilerplate,代码行数:53,代码来源:ResultWrapperHandler.cs

示例8: SDataContent

        /// <summary>
        /// SDataContent transforms OData/Web API Content/Response to SData
        /// </summary>
        /// <param name="response">The response containing the Content/Payload to transform</param>
        public SDataContent(HttpResponseMessage response)
        {
            origResponse = response;
            originalContent = origResponse.Content;

            if (originalContent == null)
            {
                throw new ArgumentNullException("response");
            }

            foreach (var header in originalContent.Headers)
            {
                Headers.Add(header.Key, header.Value);
            }

            origResponse.TryGetContentValue(out responseObject);
        }
开发者ID:jaretts,项目名称:ODataSDataDemo,代码行数:21,代码来源:SDataContent.cs

示例9: WrapResultIfNeeded

        protected virtual void WrapResultIfNeeded(HttpRequestMessage request, HttpResponseMessage response)
        {
            if (!response.IsSuccessStatusCode)
            {
                return;
            }

            var wrapAttr = HttpActionDescriptorHelper.GetWrapResultAttributeOrNull(request.GetActionDescriptor())
                           ?? DontWrapResultAttribute.Default;

            if (!wrapAttr.WrapOnSuccess)
            {
                return;
            }

            object resultObject;
            if (!response.TryGetContentValue(out resultObject))
            {
                return;
            }

            if (resultObject == null)
            {
                response.Content = new ObjectContent<AjaxResponse>(
                    new AjaxResponse(),
                    _webApiModuleConfiguration.HttpConfiguration.Formatters.JsonFormatter
                    );
            }

            if (resultObject is AjaxResponse)
            {
                return;
            }

            response.Content = new ObjectContent<AjaxResponse>(
                new AjaxResponse(resultObject),
                _webApiModuleConfiguration.HttpConfiguration.Formatters.JsonFormatter
                );
        }
开发者ID:fly-bug,项目名称:aspnetboilerplate,代码行数:39,代码来源:ResultWrapperHandler.cs


注:本文中的System.Net.Http.HttpResponseMessage.TryGetContentValue方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。