diff --git a/src/Microsoft.AspNet.Mvc.Core/ViewComponents/ContentViewComponentResult.cs b/src/Microsoft.AspNet.Mvc.Core/ViewComponents/ContentViewComponentResult.cs
index 4b2efddf57..4ef89e3ce8 100644
--- a/src/Microsoft.AspNet.Mvc.Core/ViewComponents/ContentViewComponentResult.cs
+++ b/src/Microsoft.AspNet.Mvc.Core/ViewComponents/ContentViewComponentResult.cs
@@ -7,29 +7,67 @@ 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());
}
- public string Content { get; private set; }
+ ///
+ /// Gets the content.
+ ///
+ public string Content { get; }
- public HtmlString EncodedContent { get; private set; }
+ ///
+ /// 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());
diff --git a/src/Microsoft.AspNet.Mvc.Core/ViewComponents/JsonViewComponentResult.cs b/src/Microsoft.AspNet.Mvc.Core/ViewComponents/JsonViewComponentResult.cs
index be3e34785b..ecf02ec6d5 100644
--- a/src/Microsoft.AspNet.Mvc.Core/ViewComponents/JsonViewComponentResult.cs
+++ b/src/Microsoft.AspNet.Mvc.Core/ViewComponents/JsonViewComponentResult.cs
@@ -2,29 +2,70 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Threading.Tasks;
+using Microsoft.Framework.DependencyInjection;
namespace Microsoft.AspNet.Mvc
{
+ ///
+ /// An which renders JSON text when executed.
+ ///
public class JsonViewComponentResult : IViewComponentResult
{
- public JsonViewComponentResult([NotNull] object data)
+ ///
+ /// Initializes a new .
+ ///
+ /// The value to format as JSON text.
+ public JsonViewComponentResult(object value)
{
- Data = data;
+ Value = value;
}
- public object Data { get; private set; }
+ ///
+ /// Initializes a new .
+ ///
+ /// The value to format as JSON text.
+ /// The to use.
+ public JsonViewComponentResult(object value, JsonOutputFormatter formatter)
+ {
+ Value = value;
+ Formatter = formatter;
+ }
+ ///
+ /// Gets the value.
+ ///
+ public object Value { get; }
+
+ ///
+ /// Gets the formatter.
+ ///
+ public JsonOutputFormatter Formatter { get; }
+
+ ///
+ /// Renders JSON text to the output.
+ ///
+ /// The .
public void Execute([NotNull] ViewComponentContext context)
{
- var formatter = new JsonOutputFormatter();
- formatter.WriteObject(context.Writer, Data);
+ var formatter = Formatter ?? ResolveFormatter(context);
+ formatter.WriteObject(context.Writer, Value);
}
-#pragma warning disable 1998
- public async Task ExecuteAsync([NotNull] ViewComponentContext context)
+ ///
+ /// Renders JSON text to the output.
+ ///
+ /// The .
+ /// A completed .
+ public Task ExecuteAsync([NotNull] ViewComponentContext context)
{
Execute(context);
+ return Task.FromResult(true);
+ }
+
+ private static JsonOutputFormatter ResolveFormatter(ViewComponentContext context)
+ {
+ var services = context.ViewContext.HttpContext.RequestServices;
+ return services.GetRequiredService();
}
-#pragma warning restore 1998
}
}
diff --git a/src/Microsoft.AspNet.Mvc.Core/ViewComponents/ViewComponent.cs b/src/Microsoft.AspNet.Mvc.Core/ViewComponents/ViewComponent.cs
index 01cdbabeb5..3b0f8ff59c 100644
--- a/src/Microsoft.AspNet.Mvc.Core/ViewComponents/ViewComponent.cs
+++ b/src/Microsoft.AspNet.Mvc.Core/ViewComponents/ViewComponent.cs
@@ -1,21 +1,69 @@
// 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.Security.Principal;
using Microsoft.AspNet.Http;
+using Microsoft.AspNet.Mvc.ModelBinding;
using Microsoft.AspNet.Mvc.Rendering;
+using Microsoft.AspNet.Routing;
namespace Microsoft.AspNet.Mvc
{
+ ///
+ /// A base class for view components.
+ ///
[ViewComponent]
public abstract class ViewComponent
{
private dynamic _viewBag;
+ ///
+ /// Gets the .
+ ///
public HttpContext Context
{
- get { return ViewContext == null ? null : ViewContext.HttpContext; }
+ get
+ {
+ return ViewContext?.HttpContext;
+ }
}
+ ///
+ /// Gets the .
+ ///
+ public HttpRequest Request
+ {
+ get
+ {
+ return ViewContext?.HttpContext?.Request;
+ }
+ }
+
+ ///
+ /// Gets the for the current user.
+ ///
+ public IPrincipal User
+ {
+ get
+ {
+ return ViewContext?.HttpContext?.User;
+ }
+ }
+
+ ///
+ /// Gets the for the current request.
+ ///
+ public RouteData RouteData
+ {
+ get
+ {
+ return ViewContext?.RouteData;
+ }
+ }
+
+ ///
+ /// Gets the view bag.
+ ///
public dynamic ViewBag
{
get
@@ -29,44 +77,105 @@ namespace Microsoft.AspNet.Mvc
}
}
+ ///
+ /// Gets the .
+ ///
+ public ModelStateDictionary ModelState
+ {
+ get
+ {
+ return ViewData?.ModelState;
+ }
+ }
+
+ ///
+ /// Gets or sets the .
+ ///
+ [Activate]
+ public IUrlHelper Url { get; set; }
+
+ ///
+ /// Gets or sets the .
+ ///
[Activate]
public ViewContext ViewContext { get; set; }
+ ///
+ /// Gets or sets the .
+ ///
[Activate]
public ViewDataDictionary ViewData { get; set; }
+ ///
+ /// Gets or sets the .
+ ///
[Activate]
public ICompositeViewEngine ViewEngine { get; set; }
+ ///
+ /// Returns a result which will render HTML encoded text.
+ ///
+ /// The content, will be HTML encoded before output.
+ /// A .
public ContentViewComponentResult Content([NotNull] string content)
{
return new ContentViewComponentResult(content);
}
+ ///
+ /// Returns a result which will render JSON text.
+ ///
+ /// The value to output in JSON text.
+ /// A .
public JsonViewComponentResult Json([NotNull] object value)
{
return new JsonViewComponentResult(value);
}
+ ///
+ /// Returns a result which will render the partial view with name "Default".
+ ///
+ /// A .
public ViewViewComponentResult View()
{
return View