diff --git a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewComponents/DefaultViewComponentHelper.cs b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewComponents/DefaultViewComponentHelper.cs index fff5e3e689..19221c2916 100644 --- a/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewComponents/DefaultViewComponentHelper.cs +++ b/src/Microsoft.AspNetCore.Mvc.ViewFeatures/ViewComponents/DefaultViewComponentHelper.cs @@ -135,12 +135,15 @@ namespace Microsoft.AspNetCore.Mvc.ViewComponents // Internal for testing internal IDictionary GetArgumentDictionary(ViewComponentDescriptor descriptor, object arguments) { - if (descriptor.Parameters.Count == 1 && descriptor.Parameters[0].ParameterType.IsAssignableFrom(arguments.GetType())) + if (arguments != null) { - return new Dictionary(capacity: 1, comparer: StringComparer.OrdinalIgnoreCase) + if (descriptor.Parameters.Count == 1 && descriptor.Parameters[0].ParameterType.IsAssignableFrom(arguments.GetType())) { - { descriptor.Parameters[0].Name, arguments } - }; + return new Dictionary(capacity: 1, comparer: StringComparer.OrdinalIgnoreCase) + { + { descriptor.Parameters[0].Name, arguments } + }; + } } return PropertyHelper.ObjectToDictionary(arguments); diff --git a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/ViewComponents/DefaultViewComponentHelperTest.cs b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/ViewComponents/DefaultViewComponentHelperTest.cs index 8098d6511e..ae4503c7a9 100644 --- a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/ViewComponents/DefaultViewComponentHelperTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/ViewComponents/DefaultViewComponentHelperTest.cs @@ -15,6 +15,21 @@ namespace Microsoft.AspNetCore.Mvc.ViewComponents { public class DefaultViewComponentHelperTest { + [Fact] + public void GetArgumentDictionary_SupportsNullArguments() + { + // Arrange + var helper = CreateHelper(); + var descriptor = CreateDescriptorForType(typeof(ViewComponentSingleParam)); + + // Act + var argumentDictionary = helper.GetArgumentDictionary(descriptor, null); + + // Assert + Assert.Equal(0, argumentDictionary.Count); + Assert.IsType(typeof(Dictionary),argumentDictionary); + } + [Fact] public void GetArgumentDictionary_SupportsAnonymouslyTypedArguments() {