diff --git a/src/Microsoft.AspNet.Mvc.Razor/RazorPageActivator.cs b/src/Microsoft.AspNet.Mvc.Razor/RazorPageActivator.cs index 5904c671b9..910662ca7c 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/RazorPageActivator.cs +++ b/src/Microsoft.AspNet.Mvc.Razor/RazorPageActivator.cs @@ -87,7 +87,8 @@ private PageActivationInfo CreateViewActivationInfo(Type type) PropertyActivators = PropertyActivator.GetPropertiesToActivate( type, typeof(RazorInjectAttribute), - CreateActivateInfo) + CreateActivateInfo, + includeNonPublic: true) }; } diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/DefaultControllerFactoryTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/DefaultControllerFactoryTest.cs index 9c93ea74dd..9156f8ae56 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/DefaultControllerFactoryTest.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/DefaultControllerFactoryTest.cs @@ -166,6 +166,31 @@ public void CreateController_IgnoresPropertiesThatAreNotDecoratedWithAttribute() Assert.Null(controller.ActionContext); } + [Fact] + public void CreateController_IgnoresNonPublicProperties() + { + // Arrange + var actionDescriptor = new ControllerActionDescriptor + { + ControllerTypeInfo = typeof(ControllerWithNonVisibleProperties).GetTypeInfo() + }; + var services = GetServices(); + var httpContext = new DefaultHttpContext + { + RequestServices = services + }; + var context = new ActionContext(httpContext, new RouteData(), actionDescriptor); + var factory = new DefaultControllerFactory(new DefaultControllerActivator(new DefaultTypeActivatorCache())); + + // Act + var result = factory.CreateController(context); + + // Assert + var controller = Assert.IsType(result); + Assert.Null(controller.ActionContext); + Assert.Null(controller.BindingContext); + } + [Fact] public void CreateController_ThrowsIfPropertyCannotBeActivated() { @@ -271,6 +296,13 @@ private class ControllerWithoutAttributes public ViewDataDictionary ViewData { get; set; } } + public class ControllerWithNonVisibleProperties + { + internal ActionContext ActionContext { get; set; } + + public ActionBindingContext BindingContext { get; private set; } + } + private class ControllerWithAttributes { [ActionContext] diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/ViewComponents/DefaultViewComponentActivatorTests.cs b/test/Microsoft.AspNet.Mvc.Core.Test/ViewComponents/DefaultViewComponentActivatorTests.cs index 6649c61a39..60dcf2f7e4 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/ViewComponents/DefaultViewComponentActivatorTests.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/ViewComponents/DefaultViewComponentActivatorTests.cs @@ -32,6 +32,23 @@ public void DefaultViewComponentActivator_ActivatesViewComponentContext() Assert.Same(context, instance.ViewComponentContext); } + [Fact] + public void DefaultViewComponentActivator_ActivatesViewComponentContext_IgnoresNonPublic() + { + // Arrange + var activator = new DefaultViewComponentActivator(); + + var context = new ViewComponentContext(); + var instance = new VisibilityViewComponent(); + + // Act + activator.Activate(instance, context); + + // Assert + Assert.Same(context, instance.ViewComponentContext); + Assert.Null(instance.C); + } + private class TestViewComponent : ViewComponent { public Task ExecuteAsync() @@ -39,6 +56,12 @@ public Task ExecuteAsync() throw new NotImplementedException(); } } + + private class VisibilityViewComponent : ViewComponent + { + [ViewComponentContext] + protected internal ViewComponentContext C { get; set; } + } } } #endif