This repository was archived by the owner on Nov 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 188
Expand file tree
/
Copy pathHttpResponseWritingExtensions.cs
More file actions
43 lines (40 loc) · 1.72 KB
/
HttpResponseWritingExtensions.cs
File metadata and controls
43 lines (40 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Http
{
/// <summary>
/// Convenience methods for writing to the response.
/// </summary>
public static class HttpResponseWritingExtensions
{
/// <summary>
/// Writes the given text to the response body. UTF-8 encoding will be used.
/// </summary>
/// <param name="response"></param>
/// <param name="text"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static Task WriteAsync([NotNull] this HttpResponse response, [NotNull] string text, CancellationToken cancellationToken = default(CancellationToken))
{
return response.WriteAsync(text, Encoding.UTF8, cancellationToken);
}
/// <summary>
/// Writes the given text to the response body using the given encoding.
/// </summary>
/// <param name="response"></param>
/// <param name="text"></param>
/// <param name="encoding"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static Task WriteAsync([NotNull] this HttpResponse response, [NotNull] string text, [NotNull] Encoding encoding, CancellationToken cancellationToken = default(CancellationToken))
{
byte[] data = encoding.GetBytes(text);
return response.Body.WriteAsync(data, 0, data.Length, cancellationToken);
}
}
}