Activate ViewDataDictionary in DefaultTagHelperActivator
This commit is contained in:
parent
d236d4ffde
commit
9fcf31fa43
|
|
@ -43,17 +43,22 @@ namespace Microsoft.AspNet.Mvc.Razor
|
|||
private static PropertyActivator<ViewContext> CreateActivateInfo(PropertyInfo property)
|
||||
{
|
||||
Func<ViewContext, object> valueAccessor;
|
||||
var propertyType = property.PropertyType;
|
||||
|
||||
if (property.PropertyType == typeof(ViewContext))
|
||||
if (propertyType == typeof(ViewContext))
|
||||
{
|
||||
valueAccessor = viewContext => viewContext;
|
||||
}
|
||||
else if (propertyType == typeof(ViewDataDictionary))
|
||||
{
|
||||
valueAccessor = viewContext => viewContext.ViewData;
|
||||
}
|
||||
else
|
||||
{
|
||||
valueAccessor = (viewContext) =>
|
||||
{
|
||||
var serviceProvider = viewContext.HttpContext.RequestServices;
|
||||
var service = serviceProvider.GetRequiredService(property.PropertyType);
|
||||
var service = serviceProvider.GetRequiredService(propertyType);
|
||||
|
||||
var contextable = service as ICanHasViewContext;
|
||||
contextable?.Contextualize(viewContext);
|
||||
|
|
|
|||
|
|
@ -187,6 +187,7 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
|||
"<input id=\"foo\" name=\"foo\" type=\"hidden\" value=\"test content\" />" +
|
||||
"</span>" +
|
||||
Environment.NewLine +
|
||||
"<footer>Footer from activated ViewData</footer>" +
|
||||
"</body>";
|
||||
|
||||
// Act
|
||||
|
|
|
|||
|
|
@ -56,6 +56,33 @@ namespace Microsoft.AspNet.Mvc.Razor
|
|||
Assert.NotNull(tagHelper.ViewContext);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreateTagHelper_ProvidesTagHelperWithViewData()
|
||||
{
|
||||
// Arrange
|
||||
var instance = CreateTestRazorPage();
|
||||
|
||||
// Act
|
||||
var tagHelper = instance.CreateTagHelper<ViewDataTagHelper>();
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(tagHelper.ViewData);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreateTagHelper_ProvidesTagHelperWithInternalProperties()
|
||||
{
|
||||
// Arrange
|
||||
var instance = CreateTestRazorPage();
|
||||
|
||||
// Act
|
||||
var tagHelper = instance.CreateTagHelper<TagHelperWithInternalProperty>();
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(tagHelper.ViewData);
|
||||
Assert.NotNull(tagHelper.ViewContext);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreateTagHelper_ProvidesTagHelperTypeWithViewContextAndActivates()
|
||||
{
|
||||
|
|
@ -120,12 +147,27 @@ namespace Microsoft.AspNet.Mvc.Razor
|
|||
public ViewContext ViewContext { get; set; }
|
||||
}
|
||||
|
||||
private class ViewDataTagHelper : TagHelper
|
||||
{
|
||||
[Activate]
|
||||
public ViewDataDictionary ViewData { get; set; }
|
||||
}
|
||||
|
||||
private class ViewContextServiceTagHelper : ViewContextTagHelper
|
||||
{
|
||||
[Activate]
|
||||
public MyService ActivatedService { get; set; }
|
||||
}
|
||||
|
||||
private class TagHelperWithInternalProperty : TagHelper
|
||||
{
|
||||
[Activate]
|
||||
protected internal ViewDataDictionary ViewData { get; set; }
|
||||
|
||||
[Activate]
|
||||
protected internal ViewContext ViewContext { get; set; }
|
||||
}
|
||||
|
||||
private class MyService
|
||||
{
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,24 @@
|
|||
// 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 Microsoft.AspNet.Mvc;
|
||||
using Microsoft.AspNet.Mvc.Rendering;
|
||||
using Microsoft.AspNet.Razor.Runtime.TagHelpers;
|
||||
|
||||
namespace ActivatorWebSite.TagHelpers
|
||||
{
|
||||
[HtmlElementName("body")]
|
||||
public class FooterTagHelper : TagHelper
|
||||
{
|
||||
[Activate]
|
||||
public IHtmlHelper HtmlHelper { get; set; }
|
||||
|
||||
[Activate]
|
||||
public ViewDataDictionary ViewData { get; set; }
|
||||
|
||||
public override void Process(TagHelperContext context, TagHelperOutput output)
|
||||
{
|
||||
output.PostContent = $"<footer>{HtmlHelper.Encode(ViewData["footer"])}</footer>";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -5,6 +5,7 @@
|
|||
|
||||
@{
|
||||
ViewBag.Title = "Activation Test";
|
||||
ViewData["footer"] = "Footer from activated ViewData";
|
||||
}
|
||||
|
||||
<body>
|
||||
|
|
|
|||
Loading…
Reference in New Issue