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


C# NancyContext.GetSerializationContextProvider方法代码示例

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


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

示例1: Process

        /// <summary>
        /// Process the response
        /// </summary>
        /// <param name="requestedMediaRange">Content type requested by the client</param>
        /// <param name="model">The model for the given media range</param>
        /// <param name="context">The nancy context</param>
        /// <returns>A response</returns>
        public virtual Response Process(MediaRange requestedMediaRange, dynamic model, NancyContext context)
        {
            var pomonaResponse = (PomonaResponse)model;
            string jsonString;

            if (pomonaResponse.Entity == PomonaResponse.NoBodyEntity)
                return new Response { StatusCode = pomonaResponse.StatusCode };

            jsonString =
                GetSerializerFactory(context,
                    context.GetSerializationContextProvider())
                    .GetSerializer().SerializeToString(pomonaResponse.Entity,
                        new SerializeOptions()
                        {
                            ExpandedPaths = pomonaResponse.ExpandedPaths,
                            ExpectedBaseType = pomonaResponse.ResultType
                        });

            if (IsTextHtmlContentType(requestedMediaRange))
            {
                // Wrap in html
                var response = new Response();
                HtmlJsonPrettifier.CreatePrettifiedHtmlJsonResponse(response,
                    string.Empty,
                    jsonString,
                    "http://failfailtodo");
                return response;
            }
            else
            {
                var bytes = Encoding.UTF8.GetBytes(jsonString);
                var response = new Response
                {
                    //Headers = {{"Content-Length", bytes.Length.ToString()}},
                    Contents = s => s.Write(bytes, 0, bytes.Length),
                    ContentType = ContentType,
                    StatusCode = pomonaResponse.StatusCode
                };

                if (pomonaResponse.ResponseHeaders != null)
                {
                    foreach (var kvp in pomonaResponse.ResponseHeaders)
                        response.Headers.Add(kvp);
                }

                // Add etag header
                var transformedResultType = pomonaResponse.ResultType as TransformedType;
                if (transformedResultType != null)
                {
                    var etagProperty = transformedResultType.ETagProperty;
                    if (pomonaResponse.Entity != null && etagProperty != null)
                    {
                        var etagValue = (string)etagProperty.Getter(pomonaResponse.Entity);
                        if (etagValue != null)
                        {
                            // I think defining this as a weak etag will be correct, since we can specify $expand which change data (byte-by-byte).
                            response.Headers["ETag"] = string.Format("W/\"{0}\"", etagValue);
                        }
                    }
                }

                return response;
            }
        }
开发者ID:BeeWarloc,项目名称:Pomona,代码行数:71,代码来源:PomonaResponseProcessorBase.cs


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