// 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.Threading.Tasks; using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.Internal; using Microsoft.Framework.Logging; using Microsoft.Net.Http.Headers; using Microsoft.Framework.OptionsModel; using Microsoft.AspNet.Mvc.ViewEngines; using Microsoft.AspNet.Mvc.ViewFeatures; namespace Microsoft.AspNet.Mvc { /// /// Represents an that renders a partial view to the response. /// public class PartialViewResult : ActionResult { /// /// Gets or sets the HTTP status code. /// public int? StatusCode { get; set; } /// /// Gets or sets the name of the partial view to render. /// /// /// When null, defaults to . /// public string ViewName { get; set; } /// /// Gets or sets the used for rendering the view for this result. /// public ViewDataDictionary ViewData { get; set; } /// /// Gets or sets the used for rendering the view for this result. /// public ITempDataDictionary TempData { get; set; } /// /// Gets or sets the used to locate views. /// /// When null, an instance of from /// ActionContext.HttpContext.RequestServices is used. public IViewEngine ViewEngine { get; set; } /// /// Gets or sets the representing the Content-Type header of the response. /// public MediaTypeHeaderValue ContentType { get; set; } /// public override async Task ExecuteResultAsync([NotNull] ActionContext context) { var viewEngine = ViewEngine ?? context.HttpContext.RequestServices.GetRequiredService(); var logger = context.HttpContext.RequestServices.GetRequiredService>(); var options = context.HttpContext.RequestServices.GetRequiredService>(); var viewName = ViewName ?? context.ActionDescriptor.Name; var viewEngineResult = viewEngine.FindPartialView(context, viewName); if (!viewEngineResult.Success) { logger.LogError( "The partial view '{PartialViewName}' was not found. Searched locations: {SearchedViewLocations}", viewName, viewEngineResult.SearchedLocations); } var view = viewEngineResult.EnsureSuccessful().View; logger.LogVerbose("The partial view '{PartialViewName}' was found.", viewName); if (StatusCode != null) { context.HttpContext.Response.StatusCode = StatusCode.Value; } using (view as IDisposable) { await ViewExecutor.ExecuteAsync( view, context, ViewData, TempData, options.Value.HtmlHelperOptions, ContentType); } } } }