// 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.Collections.Generic; using System.IO; using System.Text.Encodings.Web; using Microsoft.AspNet.Mvc.Rendering; using Microsoft.AspNet.Mvc.ViewFeatures; namespace Microsoft.AspNet.Mvc.ViewComponents { /// /// A context for view components. /// public class ViewComponentContext { /// /// Creates a new . /// /// /// The default constructor is provided for unit test purposes only. /// public ViewComponentContext() { ViewComponentDescriptor = new ViewComponentDescriptor(); ViewContext = new ViewContext(); } /// /// Creates a new . /// /// /// The for the view component being invoked. /// /// The view component arguments. /// The . /// The for writing output. public ViewComponentContext( ViewComponentDescriptor viewComponentDescriptor, IDictionary arguments, HtmlEncoder htmlEncoder, ViewContext viewContext, TextWriter writer) { if (viewComponentDescriptor == null) { throw new ArgumentNullException(nameof(viewComponentDescriptor)); } if (arguments == null) { throw new ArgumentNullException(nameof(arguments)); } if (htmlEncoder == null) { throw new ArgumentNullException(nameof(htmlEncoder)); } if (viewContext == null) { throw new ArgumentNullException(nameof(viewContext)); } if (writer == null) { throw new ArgumentNullException(nameof(writer)); } ViewComponentDescriptor = viewComponentDescriptor; Arguments = arguments; HtmlEncoder = htmlEncoder; // We want to create a defensive copy of the VDD here so that changes done in the VC // aren't visible in the calling view. ViewContext = new ViewContext( viewContext, viewContext.View, new ViewDataDictionary(viewContext.ViewData), writer); } /// /// Gets or sets the view component arguments. /// /// /// The property setter is provided for unit test purposes only. /// public IDictionary Arguments { get; set; } /// /// Gets or sets the . /// /// /// The property setter is provided for unit test purposes only. /// public HtmlEncoder HtmlEncoder { get; set; } /// /// Gets or sets the for the view component being invoked. /// /// /// The property setter is provided for unit test purposes only. /// public ViewComponentDescriptor ViewComponentDescriptor { get; set; } /// /// Gets or sets the . /// /// /// The property setter is provided for unit test purposes only. /// public ViewContext ViewContext { get; set; } /// /// Gets the . /// /// /// This is an alias for ViewContext.ViewData. /// public ViewDataDictionary ViewData => ViewContext.ViewData; /// /// Gets the for output. /// /// /// This is an alias for ViewContext.Writer. /// public TextWriter Writer => ViewContext.Writer; } }