// Copyright (c) .NET Foundation. 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.IO;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNet.Mvc.Rendering;
using Microsoft.Framework.Internal;
using Microsoft.Net.Http.Headers;
using Microsoft.Framework.OptionsModel;
namespace Microsoft.AspNet.Mvc
{
///
/// Utility type for rendering a to the response.
///
public static class ViewExecutor
{
private const int BufferSize = 1024;
private static readonly MediaTypeHeaderValue DefaultContentType = new MediaTypeHeaderValue("text/html")
{
Encoding = Encodings.UTF8EncodingWithoutBOM
};
///
/// Asynchronously renders the specified to the response body.
///
/// The to render.
/// The for the current executing action.
/// The for the view being rendered.
/// The for the view being rendered.
/// A that represents the asynchronous rendering.
public static async Task ExecuteAsync([NotNull] IView view,
[NotNull] ActionContext actionContext,
[NotNull] ViewDataDictionary viewData,
[NotNull] ITempDataDictionary tempData,
[NotNull] HtmlHelperOptions htmlHelperOptions,
MediaTypeHeaderValue contentType)
{
var response = actionContext.HttpContext.Response;
var contentTypeHeader = contentType;
Encoding encoding;
if (contentTypeHeader == null)
{
contentTypeHeader = DefaultContentType;
encoding = Encodings.UTF8EncodingWithoutBOM;
}
else
{
if (contentTypeHeader.Encoding == null)
{
// 1. Do not modify the user supplied content type
// 2. Parse here to handle parameters apart from charset
contentTypeHeader = MediaTypeHeaderValue.Parse(contentTypeHeader.ToString());
contentTypeHeader.Encoding = Encodings.UTF8EncodingWithoutBOM;
encoding = Encodings.UTF8EncodingWithoutBOM;
}
else
{
encoding = contentTypeHeader.Encoding;
}
}
response.ContentType = contentTypeHeader.ToString();
using (var writer = new HttpResponseStreamWriter(response.Body, encoding))
{
var viewContext = new ViewContext(
actionContext,
view,
viewData,
tempData,
writer,
htmlHelperOptions);
await view.RenderAsync(viewContext); }
}
}
}