// 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.Net; using System.Threading.Tasks; using Microsoft.AspNet.Mvc.Rendering; namespace Microsoft.AspNet.Mvc.ViewComponents { /// /// An which writes text when executed. /// /// /// always writes HTML encoded text from the /// property. /// /// When using , the provided content will be HTML /// encoded and stored in . /// /// To write pre-encoded conent, use . /// public class ContentViewComponentResult : IViewComponentResult { /// /// Initializes a new . /// /// Content to write. The content be HTML encoded when output. public ContentViewComponentResult(string content) { if (content == null) { throw new ArgumentNullException(nameof(content)); } Content = content; EncodedContent = new HtmlString(WebUtility.HtmlEncode(content)); } /// /// Initializes a new . /// /// /// Content to write. The content is treated as already HTML encoded, and no further encoding /// will be performed. /// public ContentViewComponentResult(HtmlString encodedContent) { if (encodedContent == null) { throw new ArgumentNullException(nameof(encodedContent)); } EncodedContent = encodedContent; Content = WebUtility.HtmlDecode(encodedContent.ToString()); } /// /// Gets the content. /// public string Content { get; } /// /// Gets the encoded content. /// public HtmlString EncodedContent { get; } /// /// Writes the . /// /// The . public void Execute(ViewComponentContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } context.Writer.Write(EncodedContent.ToString()); } /// /// Writes the . /// /// The . /// A completed . public Task ExecuteAsync(ViewComponentContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } return context.Writer.WriteAsync(EncodedContent.ToString()); } } }