From 261f73abc7ec77a2c81053bb74f28970770883ac Mon Sep 17 00:00:00 2001 From: Ryan Brandenburg Date: Tue, 27 Sep 2016 16:24:20 -0700 Subject: [PATCH] Add NonViewComponentAttribute --- .../NonViewComponentAttribute.cs | 16 ++++++++++++++++ .../ViewComponents/ViewComponentConventions.cs | 4 +++- .../ViewComponentConventionsTest.cs | 12 ++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 src/Microsoft.AspNetCore.Mvc.Core/NonViewComponentAttribute.cs diff --git a/src/Microsoft.AspNetCore.Mvc.Core/NonViewComponentAttribute.cs b/src/Microsoft.AspNetCore.Mvc.Core/NonViewComponentAttribute.cs new file mode 100644 index 0000000000..68eda496df --- /dev/null +++ b/src/Microsoft.AspNetCore.Mvc.Core/NonViewComponentAttribute.cs @@ -0,0 +1,16 @@ +// 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; + +namespace Microsoft.AspNetCore.Mvc +{ + /// + /// Indicates that the type and any derived types that this attribute is applied to + /// is not considered a view component by the default view component discovery mechanism. + /// + [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)] + public class NonViewComponentAttribute : Attribute + { + } +} diff --git a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewComponents/ViewComponentConventions.cs b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewComponents/ViewComponentConventions.cs index 7d07ee71bb..d8e4ce1a61 100644 --- a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewComponents/ViewComponentConventions.cs +++ b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewComponents/ViewComponentConventions.cs @@ -3,6 +3,7 @@ using System; using System.Reflection; +using Microsoft.AspNetCore.Mvc.Core; namespace Microsoft.AspNetCore.Mvc.ViewComponents { @@ -82,7 +83,8 @@ namespace Microsoft.AspNetCore.Mvc.ViewComponents if (!typeInfo.IsClass || !typeInfo.IsPublic || typeInfo.IsAbstract || - typeInfo.ContainsGenericParameters) + typeInfo.ContainsGenericParameters || + typeInfo.IsDefined(typeof(NonViewComponentAttribute))) { return false; } diff --git a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/ViewComponents/ViewComponentConventionsTest.cs b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/ViewComponents/ViewComponentConventionsTest.cs index dea32720ea..430764f79e 100644 --- a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/ViewComponents/ViewComponentConventionsTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/ViewComponents/ViewComponentConventionsTest.cs @@ -3,6 +3,7 @@ using System; using System.Reflection; +using Microsoft.AspNetCore.Mvc.Core; using Microsoft.AspNetCore.Mvc.ViewComponentConventionsTestClasses; using Xunit; @@ -41,6 +42,10 @@ namespace Microsoft.AspNetCore.Mvc.ViewComponents // Value types cannot be view components [InlineData(typeof(int), false)] + + // If it has NonViewComponent it's not a view component + [InlineData(typeof(NonViewComponentAttributeViewComponent), false)] + [InlineData(typeof(ChildOfNonViewComponent), false)] public void IsComponent(Type type, bool expected) { // Arrange & Act @@ -125,6 +130,13 @@ namespace Microsoft.AspNetCore.Mvc.ViewComponentConventionsTestClasses { } + [NonViewComponent] + public class NonViewComponentAttributeViewComponent + { } + + public class ChildOfNonViewComponent : NonViewComponentAttributeViewComponent + { } + public class NamingConventionViewComponent { }