// 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.AspNetCore.Mvc.Infrastructure; using Microsoft.AspNetCore.Mvc.Internal; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.FileProviders; using Microsoft.Net.Http.Headers; namespace Microsoft.AspNetCore.Mvc { /// /// A that on execution writes the file specified using a virtual path to the response /// using mechanisms provided by the host. /// public class VirtualFileResult : FileResult { private string _fileName; /// /// Creates a new instance with the provided /// and the provided . /// /// The path to the file. The path must be relative/virtual. /// The Content-Type header of the response. public VirtualFileResult(string fileName, string contentType) : this(fileName, MediaTypeHeaderValue.Parse(contentType)) { if (fileName == null) { throw new ArgumentNullException(nameof(fileName)); } } /// /// Creates a new instance with /// the provided and the /// provided . /// /// The path to the file. The path must be relative/virtual. /// The Content-Type header of the response. public VirtualFileResult(string fileName, MediaTypeHeaderValue contentType) : base(contentType?.ToString()) { if (fileName == null) { throw new ArgumentNullException(nameof(fileName)); } FileName = fileName; } /// /// Gets or sets the path to the file that will be sent back as the response. /// public string FileName { get { return _fileName; } set { if (value == null) { throw new ArgumentNullException(nameof(value)); } _fileName = value; } } /// /// Gets or sets the used to resolve paths. /// public IFileProvider FileProvider { get; set; } /// public override Task ExecuteResultAsync(ActionContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } var executor = context.HttpContext.RequestServices.GetRequiredService>(); return executor.ExecuteAsync(context, this); } } }