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


C# IResponse.SetContentLength方法代码示例

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


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

示例1: WriteToOutputStream

        public static bool WriteToOutputStream(IResponse response, object result, byte[] bodyPrefix, byte[] bodySuffix)
        {
            var partialResult = result as IPartialWriter;
            if (HostContext.Config.AllowPartialResponses && partialResult != null && partialResult.IsPartialRequest)
            {
                partialResult.WritePartialTo(response);
                return true;
            }

            var streamWriter = result as IStreamWriter;
            if (streamWriter != null)
            {
                if (bodyPrefix != null) response.OutputStream.Write(bodyPrefix, 0, bodyPrefix.Length);
                streamWriter.WriteTo(response.OutputStream);
                if (bodySuffix != null) response.OutputStream.Write(bodySuffix, 0, bodySuffix.Length);
                return true;
            }

            var stream = result as Stream;
            if (stream != null)
            {
                if (bodyPrefix != null) response.OutputStream.Write(bodyPrefix, 0, bodyPrefix.Length);
                stream.WriteTo(response.OutputStream);
                if (bodySuffix != null) response.OutputStream.Write(bodySuffix, 0, bodySuffix.Length);
                return true;
            }

            var bytes = result as byte[];
            if (bytes != null)
            {
                var bodyPadding = bodyPrefix != null ? bodyPrefix.Length : 0;
                if (bodySuffix != null)
                    bodyPadding += bodySuffix.Length;

                response.ContentType = MimeTypes.Binary;
                response.SetContentLength(bytes.LongLength + bodyPadding);

                if (bodyPrefix != null) response.OutputStream.Write(bodyPrefix, 0, bodyPrefix.Length);
                response.OutputStream.Write(bytes, 0, bytes.Length);
                if (bodySuffix != null) response.OutputStream.Write(bodySuffix, 0, bodySuffix.Length);
                return true;
            }

            return false;
        }
开发者ID:charygao,项目名称:ServiceStack,代码行数:45,代码来源:HttpResponseExtensionsInternal.cs

示例2: FilterResponse

        /// <summary>
        /// Filters the response.
        /// </summary>
        /// <param name="req">The req.</param>
        /// <param name="res">The res.</param>
        /// <param name="dto">The dto.</param>
        public void FilterResponse(IRequest req, IResponse res, object dto)
        {
            // Try to prevent compatibility view
            res.AddHeader("X-UA-Compatible", "IE=Edge");

            var exception = dto as Exception;

            if (exception != null)
            {
                _logger.ErrorException("Error processing request for {0}", exception, req.RawUrl);

                if (!string.IsNullOrEmpty(exception.Message))
                {
                    var error = exception.Message.Replace(Environment.NewLine, " ");
                    error = RemoveControlCharacters(error);

                    res.AddHeader("X-Application-Error-Code", error);
                }
            }

            if (dto is CompressedResult)
            {
                // Per Google PageSpeed
                // This instructs the proxies to cache two versions of the resource: one compressed, and one uncompressed. 
                // The correct version of the resource is delivered based on the client request header. 
                // This is a good choice for applications that are singly homed and depend on public proxies for user locality.                        
                res.AddHeader("Vary", "Accept-Encoding");
            }

            var hasOptions = dto as IHasOptions;

            if (hasOptions != null)
            {
                // Content length has to be explicitly set on on HttpListenerResponse or it won't be happy
                string contentLength;

                if (hasOptions.Options.TryGetValue("Content-Length", out contentLength) && !string.IsNullOrEmpty(contentLength))
                {
                    var length = long.Parse(contentLength, UsCulture);

                    if (length > 0)
                    {
                        res.SetContentLength(length);
                        
                        var listenerResponse = res.OriginalResponse as HttpListenerResponse;

                        if (listenerResponse != null)
                        {
                            // Disable chunked encoding. Technically this is only needed when using Content-Range, but
                            // anytime we know the content length there's no need for it
                            listenerResponse.SendChunked = false;
                            return;
                        }

                        var sharpResponse = res as WebSocketSharpResponse;
                        if (sharpResponse != null)
                        {
                            sharpResponse.SendChunked = false;
                        }
                    }
                }
            }
        }
开发者ID:Ceten,项目名称:MediaBrowser,代码行数:69,代码来源:ResponseFilter.cs


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