本文整理汇总了C#中Microsoft.AspNet.Http.HttpResponse类的典型用法代码示例。如果您正苦于以下问题:C# HttpResponse类的具体用法?C# HttpResponse怎么用?C# HttpResponse使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
HttpResponse类属于Microsoft.AspNet.Http命名空间,在下文中一共展示了HttpResponse类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: WriteFileAsync
/// <inheritdoc />
protected override Task WriteFileAsync(HttpResponse response, CancellationToken cancellation)
{
var bufferingFeature = response.HttpContext.Features.Get<IHttpBufferingFeature>();
bufferingFeature?.DisableResponseBuffering();
return response.Body.WriteAsync(FileContents, 0, FileContents.Length, cancellation);
}
示例2: WriteFileAsync
/// <inheritdoc />
protected override async Task WriteFileAsync(HttpResponse response)
{
if (!Path.IsPathRooted(FileName))
{
throw new NotSupportedException(Resources.FormatFileResult_PathNotRooted(FileName));
}
var sendFile = response.HttpContext.Features.Get<IHttpSendFileFeature>();
if (sendFile != null)
{
await sendFile.SendFileAsync(
FileName,
offset: 0,
length: null,
cancellation: default(CancellationToken));
}
else
{
var fileStream = GetFileStream(FileName);
using (fileStream)
{
await fileStream.CopyToAsync(response.Body, DefaultBufferSize);
}
}
}
示例3: ToHttpResponse
internal static HttpResponsePacket ToHttpResponse(HttpResponse response, ServiceMessage msg)
{
var rsp = new HttpResponsePacket();
foreach (var hdr in response.Headers)
{
// TODO: Fix adding response headers
//AddHttpHeader(hdr);
}
//TODO: Decide if to read mostly from ServiceMessage or from response.
//rsp.Version = response.... //TODO: Add a default version here
rsp.StatusCode = (int)response.StatusCode;
rsp.StatusDescription = ((IHttpResponseFeature)msg).ReasonPhrase;
if (response.Body != null)
{
using (MemoryStream ms = new MemoryStream())
{
response.Body.Position = 0;
response.Body.CopyTo(ms);
rsp.Content = ms.ToArray();
}
}
return rsp;
}
示例4: WriteFileAsync
/// <inheritdoc />
protected override Task WriteFileAsync(HttpResponse response)
{
var bufferingFeature = response.HttpContext.Features.Get<IHttpBufferingFeature>();
bufferingFeature?.DisableResponseBuffering();
return response.Body.WriteAsync(FileContents, offset: 0, count: FileContents.Length);
}
示例5: WriteFileAsync
/// <inheritdoc />
protected override async Task WriteFileAsync(HttpResponse response, CancellationToken cancellation)
{
if (!Path.IsPathRooted(FileName))
{
throw new FileNotFoundException(Resources.FormatFileResult_InvalidPath(FileName), FileName);
}
var sendFile = response.HttpContext.Features.Get<IHttpSendFileFeature>();
if (sendFile != null)
{
await sendFile.SendFileAsync(
FileName,
offset: 0,
length: null,
cancellation: cancellation);
return;
}
else
{
var fileStream = GetFileStream(FileName);
using (fileStream)
{
await fileStream.CopyToAsync(response.Body, DefaultBufferSize, cancellation);
}
return;
}
}
示例6: WriteFileAsync
/// <inheritdoc />
protected async override Task WriteFileAsync(HttpResponse response, CancellationToken cancellation)
{
var outputStream = response.Body;
using (FileStream)
{
await FileStream.CopyToAsync(outputStream, BufferSize, cancellation);
}
}
示例7: SetCacheHeaders
private void SetCacheHeaders(HttpResponse response)
{
if (_options.CacheLength != null)
{
var expires = DateTime.Now.AddSeconds(_options.CacheLength.TotalSeconds);
response.Headers.SetCommaSeparatedValues("Cache-Control", "public", $"max-age={_options.CacheLength.TotalSeconds}");
response.Headers.Set("Expires", expires.ToUniversalTime().ToString("R"));
}
}
示例8: RespondWithSwaggerJson
private void RespondWithSwaggerJson(HttpResponse response, SwaggerDocument swagger)
{
response.StatusCode = 200;
response.ContentType = "application/json";
using (var writer = new StreamWriter(response.Body))
{
_swaggerSerializer.Serialize(writer, swagger);
}
}
示例9: SerializeResponseObject
private void SerializeResponseObject(HttpResponse response, object value)
{
using (var writer = new StreamWriter(response.Body))
{
using (var jsonWriter = new JsonTextWriter(writer))
{
jsonWriter.CloseOutput = false;
var jsonSerializer = JsonSerializer.Create(/*TODO: SerializerSettings*/);
jsonSerializer.Serialize(jsonWriter, value);
}
}
}
示例10: WriteFileAsync
/// <inheritdoc />
protected async override Task WriteFileAsync(HttpResponse response, CancellationToken cancellation)
{
var outputStream = response.Body;
using (FileStream)
{
var bufferingFeature = response.HttpContext.Features.Get<IHttpBufferingFeature>();
bufferingFeature?.DisableResponseBuffering();
await FileStream.CopyToAsync(outputStream, BufferSize, cancellation);
}
}
示例11: WriteResponseBodyAsync
internal static Task WriteResponseBodyAsync(HttpResponse response, string uid, DateTimeOffset? datetime, TimeSpan? duration, string summary, string description, string location) {
return response.WriteAsync(
"BEGIN:VCALENDAR\r\n" +
"VERSION:2.0\r\n" +
"BEGIN:VEVENT\r\n" +
"UID:" + uid + "\r\n" +
"DTSTART:" + datetime?.ToString(DateTimeFormat) + "\r\n" +
"DTEND:" + datetime?.Add(duration ?? TimeSpan.Zero).ToString(DateTimeFormat) + "\r\n" +
"SUMMARY:" + summary + "\r\n" +
"DESCRIPTION:" + description + "\r\n" +
"LOCATION:" + location +
"END:VEVENT\r\n" +
"END:VCALENDAR\r\n");
}
示例12: AddLocationHeaderToMapping
public static void AddLocationHeaderToMapping(
HttpResponse response,
IDictionary<string, string> contentIdToLocationMapping,
string contentId)
{
//Contract.Assert(response != null);
//Contract.Assert(contentIdToLocationMapping != null);
//Contract.Assert(contentId != null);
//if (response.Headers.Location != null)
//{
// contentIdToLocationMapping.Add(contentId, response.Headers.Location.AbsoluteUri);
//}
throw new NotImplementedException("AddLocationHeaderToMapping");
}
示例13: WriteOutput
public static async Task WriteOutput(HttpResponse Response, string Title, Head.Tag[] 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) {
foreach (var a in HeadTags) {
sb.Append(a.Output());
}
}
sb.Append("</head><body>");
sb.Append(Body);
sb.Append("</body></html>");
await Response.WriteAsync(sb.ToString());
}
示例14: WriteFileAsync
/// <inheritdoc />
protected override Task WriteFileAsync(HttpResponse response, CancellationToken cancellation)
{
var fileProvider = GetFileProvider(response.HttpContext.RequestServices);
var resolveFilePathResult = ResolveFilePath(fileProvider);
if (resolveFilePathResult.PhysicalFilePath != null)
{
return CopyPhysicalFileToResponseAsync(response, resolveFilePathResult.PhysicalFilePath, cancellation);
}
else
{
// Example: An embedded resource
var sourceStream = resolveFilePathResult.FileInfo.CreateReadStream();
return CopyStreamToResponseAsync(sourceStream, response, cancellation);
}
}
示例15: CreateMockActionContext
private static ActionContext CreateMockActionContext(
HttpResponse response = null,
string requestAcceptHeader = "application/*",
string requestContentType = "application/json",
string requestAcceptCharsetHeader = "",
bool respectBrowserAcceptHeader = false)
{
var httpContext = new Mock<HttpContext>();
if (response != null)
{
httpContext.Setup(o => o.Response).Returns(response);
}
var content = "{name: 'Person Name', Age: 'not-an-age'}";
var contentBytes = Encoding.UTF8.GetBytes(content);
var request = new DefaultHttpContext().Request;
request.Headers["Accept-Charset"] = requestAcceptCharsetHeader;
request.Headers["Accept"] = requestAcceptHeader;
request.ContentType = requestContentType;
request.Body = new MemoryStream(contentBytes);
httpContext.Setup(o => o.Request).Returns(request);
httpContext.Setup(o => o.RequestServices).Returns(GetServiceProvider());
var optionsAccessor = new MockMvcOptionsAccessor();
optionsAccessor.Options.OutputFormatters.Add(new StringOutputFormatter());
optionsAccessor.Options.OutputFormatters.Add(new JsonOutputFormatter());
optionsAccessor.Options.RespectBrowserAcceptHeader = respectBrowserAcceptHeader;
var mockContextAccessor = new Mock<IScopedInstance<ActionBindingContext>>();
mockContextAccessor
.SetupGet(o => o.Value)
.Returns(new ActionBindingContext()
{
OutputFormatters = optionsAccessor.Options.OutputFormatters
});
httpContext.Setup(o => o.RequestServices.GetService(typeof(IScopedInstance<ActionBindingContext>)))
.Returns(mockContextAccessor.Object);
httpContext.Setup(o => o.RequestServices.GetService(typeof(IOptions<MvcOptions>)))
.Returns(optionsAccessor);
httpContext.Setup(o => o.RequestServices.GetService(typeof(ILogger<ObjectResult>)))
.Returns(new Mock<ILogger<ObjectResult>>().Object);
return new ActionContext(httpContext.Object, new RouteData(), new ActionDescriptor());
}