本文整理汇总了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;
}
示例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;
}
}
}
}
}