// 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.Net; using System.Threading.Tasks; using Microsoft.AspNet.Mvc.Rendering; namespace Microsoft.AspNet.Mvc { /// /// 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([NotNull] string 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([NotNull] HtmlString 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([NotNull] ViewComponentContext context) { context.Writer.Write(EncodedContent.ToString()); } /// /// Writes the . /// /// The . /// A completed . public async Task ExecuteAsync([NotNull] ViewComponentContext context) { await context.Writer.WriteAsync(EncodedContent.ToString()); } } }