// 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.AspNet.Mvc.ViewEngines;
using Microsoft.AspNet.Mvc.ViewFeatures;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Net.Http.Headers;
namespace Microsoft.AspNet.Mvc
{
///
/// Represents an that renders a view to the response.
///
public class ViewResult : ActionResult
{
///
/// Gets or sets the HTTP status code.
///
public int? StatusCode { get; set; }
///
/// Gets or sets the name of the view to render.
///
///
/// When null, defaults to .
///
public string ViewName { get; set; }
///
/// Gets the view data model.
///
public object Model => ViewData?.Model;
///
/// Gets or sets the for this result.
///
public ViewDataDictionary ViewData { get; set; }
///
/// Gets or sets the 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(ActionContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
var services = context.HttpContext.RequestServices;
var executor = services.GetRequiredService();
var result = executor.FindView(context, this);
result.EnsureSuccessful(originalLocations: null);
var view = result.View;
using (view as IDisposable)
{
await executor.ExecuteAsync(context, view, this);
}
}
}
}