This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fixes #4087] Add support for AddTagHelpersAsServices()
- Loading branch information
Showing
22 changed files
with
763 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/Microsoft.AspNetCore.Mvc.Razor/Internal/ServiceBasedTagHelperActivator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// 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; | ||
using Microsoft.AspNetCore.Mvc.Rendering; | ||
using Microsoft.AspNetCore.Razor.TagHelpers; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Microsoft.AspNetCore.Mvc.Razor.Internal | ||
{ | ||
/// <summary> | ||
/// A <see cref="ITagHelperActivator"/> that retrieves tag helpers as services from the request's | ||
/// <see cref="IServiceProvider"/>. | ||
/// </summary> | ||
public class ServiceBasedTagHelperActivator : ITagHelperActivator | ||
{ | ||
/// <inheritdoc /> | ||
public TTagHelper Create<TTagHelper>(ViewContext context) where TTagHelper : ITagHelper | ||
{ | ||
if (context == null) | ||
{ | ||
throw new ArgumentNullException(nameof(context)); | ||
} | ||
|
||
return context.HttpContext.RequestServices.GetRequiredService<TTagHelper>(); | ||
} | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
src/Microsoft.AspNetCore.Mvc.Razor/TagHelpers/FeatureTagHelperTypeResolver.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// 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; | ||
using System.Collections.Generic; | ||
using Microsoft.AspNetCore.Razor; | ||
using Microsoft.AspNetCore.Mvc.ApplicationParts; | ||
using Microsoft.AspNetCore.Razor.Runtime.TagHelpers; | ||
using System.Reflection; | ||
|
||
namespace Microsoft.AspNetCore.Mvc.Razor.TagHelpers | ||
{ | ||
/// <summary> | ||
/// Resolves tag helper types from the <see cref="ApplicationPartManager.ApplicationParts"/> | ||
/// of the application. | ||
/// </summary> | ||
public class FeatureTagHelperTypeResolver : ITagHelperTypeResolver | ||
{ | ||
private readonly ApplicationPartManager _manager; | ||
|
||
/// <summary> | ||
/// Initializes a new <see cref="FeatureTagHelperTypeResolver"/> instance. | ||
/// </summary> | ||
/// <param name="manager">The <see cref="ApplicationPartManager"/> of the application.</param> | ||
public FeatureTagHelperTypeResolver(ApplicationPartManager manager) | ||
{ | ||
if (manager == null) | ||
{ | ||
throw new ArgumentNullException(nameof(manager)); | ||
} | ||
|
||
_manager = manager; | ||
|
||
} | ||
|
||
/// <inheritdoc /> | ||
public IEnumerable<Type> Resolve(string assemblyName, SourceLocation documentLocation, ErrorSink errorSink) | ||
{ | ||
if (assemblyName == null) | ||
{ | ||
throw new ArgumentNullException(nameof(assemblyName)); | ||
} | ||
|
||
if (errorSink == null) | ||
{ | ||
throw new ArgumentNullException(nameof(errorSink)); | ||
} | ||
|
||
var parsedName = new AssemblyName(assemblyName); | ||
Assembly assembly; | ||
try | ||
{ | ||
assembly = Assembly.Load(parsedName); | ||
} | ||
catch (Exception ex) | ||
{ | ||
errorSink.OnError( | ||
documentLocation, | ||
string.Format( | ||
"Cannot resolve TagHelper containing assembly '{0}'.Error: {1}", | ||
parsedName.Name, | ||
ex.Message), | ||
assemblyName.Length); | ||
|
||
return Type.EmptyTypes; | ||
} | ||
|
||
var feature = new TagHelperFeature(); | ||
_manager.PopulateFeature(feature); | ||
|
||
var results = new List<Type>(); | ||
for (int i = 0; i < feature.TagHelpers.Count; i++) | ||
{ | ||
var tagHelper = feature.TagHelpers[i]; | ||
var tagHelperAssembly = tagHelper.Assembly; | ||
|
||
if (tagHelperAssembly.Equals(assembly)) | ||
{ | ||
results.Add(tagHelper.AsType()); | ||
} | ||
} | ||
|
||
return results; | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/Microsoft.AspNetCore.Mvc.Razor/TagHelpers/TagHelperFeature.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// 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.Reflection; | ||
using Microsoft.AspNetCore.Mvc.ApplicationParts; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Microsoft.AspNetCore.Mvc.Razor.TagHelpers | ||
{ | ||
/// <summary> | ||
/// The list of tag helper types in an MVC application. The <see cref="TagHelperFeature"/> can be populated | ||
/// using the <see cref="ApplicationPartManager"/> that is available during startup at <see cref="IMvcBuilder.PartManager"/> | ||
/// and <see cref="IMvcCoreBuilder.PartManager"/> or at a later stage by requiring the <see cref="ApplicationPartManager"/> | ||
/// as a dependency in a component. | ||
/// </summary> | ||
public class TagHelperFeature | ||
{ | ||
public IList<TypeInfo> TagHelpers { get; } = new List<TypeInfo>(); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/Microsoft.AspNetCore.Mvc.Razor/TagHelpers/TagHelperFeatureProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// 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 Microsoft.AspNetCore.Mvc.ApplicationParts; | ||
using Microsoft.AspNetCore.Razor.Runtime.TagHelpers; | ||
|
||
namespace Microsoft.AspNetCore.Mvc.Razor.TagHelpers | ||
{ | ||
/// <summary> | ||
/// Discovers tag helpers from a list of <see cref="ApplicationPart"/> instances. | ||
/// </summary> | ||
public class TagHelperFeatureProvider : IApplicationFeatureProvider<TagHelperFeature> | ||
{ | ||
/// <inheritdoc /> | ||
public void PopulateFeature(IEnumerable<ApplicationPart> parts, TagHelperFeature feature) | ||
{ | ||
foreach (var type in parts.OfType<IApplicationPartTypeProvider>().SelectMany(p => p.Types)) | ||
{ | ||
if (TagHelperConventions.IsTagHelper(type) && ! feature.TagHelpers.Contains(type)) | ||
{ | ||
feature.TagHelpers.Add(type); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
test/Microsoft.AspNetCore.Mvc.FunctionalTests/TagHelpersFromServicesTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// 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.Net.Http; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace Microsoft.AspNetCore.Mvc.FunctionalTests | ||
{ | ||
public class TagHelpersFromServicesTest : IClassFixture<MvcTestFixture<ControllersFromServicesWebSite.Startup>> | ||
{ | ||
public TagHelpersFromServicesTest(MvcTestFixture<ControllersFromServicesWebSite.Startup> fixture) | ||
{ | ||
Client = fixture.Client; | ||
} | ||
|
||
public HttpClient Client { get; } | ||
|
||
[Fact] | ||
public async Task TagHelpersWithConstructorInjectionAreCreatedAndActivated() | ||
{ | ||
// Arrange | ||
var expected = "3"; | ||
var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/another/inservicestaghelper"); | ||
|
||
// Act | ||
var response = await Client.SendAsync(request); | ||
var responseText = await response.Content.ReadAsStringAsync(); | ||
|
||
// Assert | ||
Assert.Equal(expected, responseText.Trim()); | ||
} | ||
} | ||
} |
Oops, something went wrong.