Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Commit

Permalink
Add back support for AddTagHelpersAsServices
Browse files Browse the repository at this point in the history
This doesn't go through the Razor tag helper discovery pipeline because
this can really only ever work for ITagHelper based taghelpers. So
there's really no point in reusing that logic, which would be hard
anyway.
  • Loading branch information
rynowak committed May 23, 2017
1 parent 53c56f5 commit a6d97d3
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ private static void AddRazorViewEngineFeatureProviders(IMvcCoreBuilder builder)
builder.PartManager.FeatureProviders.Add(new MetadataReferenceFeatureProvider());
}

if (!builder.PartManager.FeatureProviders.OfType<TagHelperFeatureProvider>().Any())
{
builder.PartManager.FeatureProviders.Add(new TagHelperFeatureProvider());
}

if (!builder.PartManager.FeatureProviders.OfType<ViewsFeatureProvider>().Any())
{
builder.PartManager.FeatureProviders.Add(new ViewsFeatureProvider());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// 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.Collections.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.AspNetCore.Mvc.ApplicationParts;
using Microsoft.AspNetCore.Razor.TagHelpers;

namespace Microsoft.AspNetCore.Mvc.Razor.TagHelpers
{
public class TagHelperFeatureProvider : IApplicationFeatureProvider<TagHelperFeature>
{
public void PopulateFeature(IEnumerable<ApplicationPart> parts, TagHelperFeature feature)
{
foreach (var part in parts)
{
if (IncludePart(part) && part is IApplicationPartTypeProvider typeProvider)
{
foreach (var type in typeProvider.Types)
{
var typeInfo = type.GetTypeInfo();
if (IncludeType(typeInfo) && !feature.TagHelpers.Contains(typeInfo))
{
feature.TagHelpers.Add(typeInfo);
}
}
}
}
}

protected virtual bool IncludePart(ApplicationPart part) => true;

protected virtual bool IncludeType(TypeInfo type)
{
// We don't need to check visibility here, that's handled by the type provider.
return
typeof(ITagHelper).GetTypeInfo().IsAssignableFrom(type) &&
!type.IsAbstract &&
!type.IsGenericType;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public TagHelpersFromServicesTest(MvcTestFixture<ControllersFromServicesWebSite.

public HttpClient Client { get; }

[Fact(Skip = "Workaround for https://github.com/aspnet/Mvc/issues/5768.")]
[Fact]
public async Task TagHelpersWithConstructorInjectionAreCreatedAndActivated()
{
// Arrange
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void AddTagHelpersAsServices_RegistersDiscoveredTagHelpers()
typeof(TestTagHelperOne),
typeof(TestTagHelperTwo)));

manager.FeatureProviders.Add(new TestFeatureProvider());
manager.FeatureProviders.Add(new TagHelperFeatureProvider());

var builder = new MvcBuilder(services, manager);

Expand Down Expand Up @@ -78,16 +78,5 @@ private class TestTagHelperOne : TagHelper
private class TestTagHelperTwo : TagHelper
{
}

private class TestFeatureProvider : IApplicationFeatureProvider<TagHelperFeature>
{
public void PopulateFeature(IEnumerable<ApplicationPart> parts, TagHelperFeature feature)
{
foreach (var type in parts.OfType<IApplicationPartTypeProvider>().SelectMany(tp => tp.Types))
{
feature.TagHelpers.Add(type);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using Microsoft.AspNetCore.Mvc.Razor.Compilation;
using Microsoft.AspNetCore.Mvc.Razor.Internal;
using Microsoft.AspNetCore.Mvc.Razor.TagHelpers;
using Microsoft.AspNetCore.Razor.Runtime.TagHelpers;
using Microsoft.AspNetCore.Razor.TagHelpers;
using Microsoft.Extensions.DependencyInjection;
using Moq;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ public void AddMvcTwice_DoesNotAddApplicationFeatureProvidersTwice()
feature => Assert.IsType<ControllerFeatureProvider>(feature),
feature => Assert.IsType<ViewComponentFeatureProvider>(feature),
feature => Assert.IsType<MetadataReferenceFeatureProvider>(feature),
feature => Assert.IsType<TagHelperFeatureProvider>(feature),
feature => Assert.IsType<ViewsFeatureProvider>(feature),
feature => Assert.IsType<CompiledPageFeatureProvider>(feature));
}
Expand Down

0 comments on commit a6d97d3

Please sign in to comment.