本文整理汇总了C#中HttpResponse.WriteAsync方法的典型用法代码示例。如果您正苦于以下问题:C# HttpResponse.WriteAsync方法的具体用法?C# HttpResponse.WriteAsync怎么用?C# HttpResponse.WriteAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HttpResponse
的用法示例。
在下文中一共展示了HttpResponse.WriteAsync方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: WriteVideoAsync
protected async Task WriteVideoAsync(HttpResponse response)
{
var bufferingFeature = response.HttpContext.Features.Get<IHttpBufferingFeature>();
bufferingFeature?.DisableResponseBuffering();
var length = FileStream.Length;
var rangeHeaderValue = response.HttpContext.GetRanges(length);
var isMultipart = IsMultipartRequest(rangeHeaderValue);
if (isMultipart)
{
response.ContentType = $"multipart/byteranges; boundary={MultipartBoundary}";
}
else
{
response.ContentType = ContentType.ToString();
}
response.Headers.Add("Accept-Ranges", "bytes");
if (IsRangeRequest(rangeHeaderValue))
{
response.StatusCode = (int)HttpStatusCode.PartialContent;
foreach (var range in rangeHeaderValue.Ranges)
{
if (isMultipart)
{
await response.WriteAsync($"--{MultipartBoundary}{Environment.NewLine}");
await response.WriteAsync($"Content-type: {ContentType}{Environment.NewLine}");
await response.WriteAsync($"Content-Range: bytes {range.From}-{range.To}/{length}{Environment.NewLine}");
}
else
{
response.Headers.Add("Content-Range", $"bytes {range.From}-{range.To}/{length}");
}
await WriteDataToResponseBody(response, range);
if (isMultipart)
{
await response.WriteAsync($"{Environment.NewLine}--{MultipartBoundary}--{Environment.NewLine}");
}
}
}
else
{
await FileStream.CopyToAsync(response.Body);
}
}
示例2: DumpConfig
private static async Task DumpConfig(HttpResponse response, IConfiguration config, string indentation = "")
{
foreach (var child in config.GetSubKeys())
{
await response.WriteAsync(indentation + "[" + child.Key + "] " + config.Get(child.Key) + "\r\n");
await DumpConfig(response, child.Value, indentation + " ");
}
}
示例3: WriteOutput
public static async Task WriteOutput(HttpResponse Response, string Title, string HeadTags, string Body) {
var sb = new StringBuilder();
sb.Append("<!DOCTYPE html><head><meta charset=\"utf-8\">");
if (Title != null) {
sb.Append("<title>" + Title + "</title>");
}
if (HeadTags != null) {
sb.Append(HeadTags);
}
sb.Append("</head><body>");
sb.Append(Body);
sb.Append("</body></html>");
await Response.WriteAsync(sb.ToString());
}
示例4: FilterAsync
//.........这里部分代码省略.........
FilePath = FilePath,
});
}
catch (FileNotFoundException)
{
throw (new HttpException(HttpCode.NOT_FOUND_404));
}
}
if (Cache.Enabled)
{
await Console.Out.WriteLineAsync(String.Format("Caching '{0}'", FilePath));
}
var FileInfo = await VirtualFileSystem.GetFileInfoAsync(FilePath);
if (FileInfo.Exists)
{
bool ShouldCacheInMemory = FileInfo.Length <= CacheSizeThresold;
string ETag = null;
byte[] Data = null;
if (ShouldCacheInMemory)
{
Data = await VirtualFileSystem.ReadAsBytesAsync(FilePath);
ETag = BitConverter.ToString(MD5.Create().ComputeHash(Data));
}
return new ResultStruct()
{
RealFilePath = FilePath,
ContentType = MimeType.GetFromPath(FilePath),
FileInfo = FileInfo,
ETag = ETag,
Data = Data,
Exists = true,
};
}
else
{
throw (new HttpException(HttpCode.NOT_FOUND_404));
}
});
Response.Headers["Content-Type"] = CachedResult.ContentType;
if (CachedResult.ETag != null)
{
Response.Headers["ETag"] = CachedResult.ETag;
}
Response.Headers["Date"] = DateTime.UtcNow.ToString("R", CultureInfo.InvariantCulture);
Response.Headers["Last-Modified"] = CachedResult.FileInfo.LastWriteTimeUtc.ToString("R", CultureInfo.InvariantCulture);
Response.Headers["Cache-Control"] = "max-age=2419200, private";
Response.Headers["Expires"] = "Wed, 11 Apr 2022 18:23:41 GMT";
// Check ETag
if (Request.Headers["If-None-Match"] != "")
{
if (Request.Headers["If-None-Match"] == CachedResult.ETag)
{
throw(new HttpException(HttpCode.NOT_MODIFIED_304));
}
}
// Check Last-Modified
if (Request.Headers["If-Modified-Since"] != "")
{
var RequestIfModifiedSince = DateTime.ParseExact(Request.Headers["If-Modified-Since"], "R", CultureInfo.InvariantCulture);
if (RequestIfModifiedSince == CachedResult.FileInfo.LastWriteTimeUtc)
{
throw(new HttpException(HttpCode.NOT_MODIFIED_304));
}
}
Response.Buffering = true;
Response.ChunkedTransferEncoding = false;
/*
Cache-Control:max-age=2419200, private
Connection:keep-alive
Date:Wed, 14 Mar 2012 14:52:54 GMT
Expires:Wed, 11 Apr 2012 14:52:54 GMT
Last-Modified:Wed, 11 Jan 2012 13:52:46 GMT
*/
Response.Headers["Content-Length"] = CachedResult.FileInfo.Length.ToString();
// Cached byte[]
if (CachedResult.Data != null)
{
await Response.WriteAsync(CachedResult.Data);
}
// No cached byte[], stream the file
else
{
Response.Buffering = false;
//Response.ChunkedTransferEncoding = true;
await Response.StreamFileASync(CachedResult.RealFilePath);
}
}
示例5: WriteContentTypeUnsupported
private async Task WriteContentTypeUnsupported(HttpResponse response, string contentTypeReturned, Dictionary<string, string> headers)
{
response.StatusCode = (int)HttpStatusCode.NotFound;
await WriteHeaders(response, headers);
await response.WriteAsync(string.Format("Non-Image content-type returned '{0}'", contentTypeReturned));
}
示例6: WriteContentLengthExceeded
private async Task WriteContentLengthExceeded(HttpResponse response, Dictionary<string, string> headers)
{
response.StatusCode = (int)HttpStatusCode.NotFound;
await WriteHeaders(response, headers);
await response.WriteAsync("Content-Length exceeded");
}
示例7: WriteInvalidSignature
private async Task WriteInvalidSignature(HttpResponse response, string url, string signature)
{
response.StatusCode = (int)HttpStatusCode.NotFound;
await response.WriteAsync(string.Format("checksum mismatch {0}:{1}", url, signature));
}