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)
|
private static PropertyActivator<ViewContext> CreateActivateInfo(PropertyInfo property)
|
||||||
{
|
{
|
||||||
Func<ViewContext, object> valueAccessor;
|
Func<ViewContext, object> valueAccessor;
|
||||||
|
var propertyType = property.PropertyType;
|
||||||
|
|
||||||
if (property.PropertyType == typeof(ViewContext))
|
if (propertyType == typeof(ViewContext))
|
||||||
{
|
{
|
||||||
valueAccessor = viewContext => viewContext;
|
valueAccessor = viewContext => viewContext;
|
||||||
}
|
}
|
||||||
|
else if (propertyType == typeof(ViewDataDictionary))
|
||||||
|
{
|
||||||
|
valueAccessor = viewContext => viewContext.ViewData;
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
valueAccessor = (viewContext) =>
|
valueAccessor = (viewContext) =>
|
||||||
{
|
{
|
||||||
var serviceProvider = viewContext.HttpContext.RequestServices;
|
var serviceProvider = viewContext.HttpContext.RequestServices;
|
||||||
var service = serviceProvider.GetRequiredService(property.PropertyType);
|
var service = serviceProvider.GetRequiredService(propertyType);
|
||||||
|
|
||||||
var contextable = service as ICanHasViewContext;
|
var contextable = service as ICanHasViewContext;
|
||||||
contextable?.Contextualize(viewContext);
|
contextable?.Contextualize(viewContext);
|
||||||
|
|
|
||||||
|
|
@ -187,6 +187,7 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||||
"<input id=\"foo\" name=\"foo\" type=\"hidden\" value=\"test content\" />" +
|
"<input id=\"foo\" name=\"foo\" type=\"hidden\" value=\"test content\" />" +
|
||||||
"</span>" +
|
"</span>" +
|
||||||
Environment.NewLine +
|
Environment.NewLine +
|
||||||
|
"<footer>Footer from activated ViewData</footer>" +
|
||||||
"</body>";
|
"</body>";
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
|
|
|
||||||
|
|
@ -56,6 +56,33 @@ namespace Microsoft.AspNet.Mvc.Razor
|
||||||
Assert.NotNull(tagHelper.ViewContext);
|
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]
|
[Fact]
|
||||||
public void CreateTagHelper_ProvidesTagHelperTypeWithViewContextAndActivates()
|
public void CreateTagHelper_ProvidesTagHelperTypeWithViewContextAndActivates()
|
||||||
{
|
{
|
||||||
|
|
@ -120,12 +147,27 @@ namespace Microsoft.AspNet.Mvc.Razor
|
||||||
public ViewContext ViewContext { get; set; }
|
public ViewContext ViewContext { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private class ViewDataTagHelper : TagHelper
|
||||||
|
{
|
||||||
|
[Activate]
|
||||||
|
public ViewDataDictionary ViewData { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
private class ViewContextServiceTagHelper : ViewContextTagHelper
|
private class ViewContextServiceTagHelper : ViewContextTagHelper
|
||||||
{
|
{
|
||||||
[Activate]
|
[Activate]
|
||||||
public MyService ActivatedService { get; set; }
|
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
|
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";
|
ViewBag.Title = "Activation Test";
|
||||||
|
ViewData["footer"] = "Footer from activated ViewData";
|
||||||
}
|
}
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue