aspnetcore/src/Microsoft.AspNet.Mvc.Core/ActionResults/PartialViewResult.cs

64 lines
2.3 KiB
C#

// 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.Threading.Tasks;
using Microsoft.AspNet.Mvc.Rendering;
using Microsoft.Framework.DependencyInjection;
namespace Microsoft.AspNet.Mvc
{
/// <summary>
/// Represents an <see cref="ActionResult"/> that renders a partial view to the response.
/// </summary>
public class PartialViewResult : ActionResult
{
/// <summary>
/// Gets or sets the HTTP status code.
/// </summary>
public int? StatusCode { get; set; }
/// <summary>
/// Gets or sets the name of the partial view to render.
/// </summary>
/// <remarks>
/// When <c>null</c>, defaults to <see cref="ActionDescriptor.Name"/>.
/// </remarks>
public string ViewName { get; set; }
/// <summary>
/// Gets or sets the <see cref="ViewDataDictionary"/> used for rendering the view for this result.
/// </summary>
public ViewDataDictionary ViewData { get; set; }
/// <summary>
/// Gets or sets the <see cref="IViewEngine"/> used to locate views.
/// </summary>
/// <remarks>When <c>null</c>, an instance of <see cref="ICompositeViewEngine"/> from
/// <c>ActionContext.HttpContext.RequestServices</c> is used.</remarks>
public IViewEngine ViewEngine { get; set; }
/// <inheritdoc />
public override async Task ExecuteResultAsync([NotNull] ActionContext context)
{
var viewEngine = ViewEngine ??
context.HttpContext.RequestServices.GetRequiredService<ICompositeViewEngine>();
var viewName = ViewName ?? context.ActionDescriptor.Name;
var view = viewEngine.FindPartialView(context, viewName)
.EnsureSuccessful()
.View;
if (StatusCode != null)
{
context.HttpContext.Response.StatusCode = StatusCode.Value;
}
using (view as IDisposable)
{
await ViewExecutor.ExecuteAsync(view, context, ViewData, contentType: null);
}
}
}
}