Fix #2278 - Only activate public properties

This commit is contained in:
Ryan Nowak 2015-06-04 16:16:25 -07:00
parent f5cabf2029
commit a452b10ba4
3 changed files with 57 additions and 1 deletions

View File

@ -87,7 +87,8 @@ namespace Microsoft.AspNet.Mvc.Razor
PropertyActivators = PropertyActivator<ViewContext>.GetPropertiesToActivate(
type,
typeof(RazorInjectAttribute),
CreateActivateInfo)
CreateActivateInfo,
includeNonPublic: true)
};
}

View File

@ -166,6 +166,31 @@ namespace Microsoft.AspNet.Mvc.Core
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<ControllerWithNonVisibleProperties>(result);
Assert.Null(controller.ActionContext);
Assert.Null(controller.BindingContext);
}
[Fact]
public void CreateController_ThrowsIfPropertyCannotBeActivated()
{
@ -271,6 +296,13 @@ namespace Microsoft.AspNet.Mvc.Core
public ViewDataDictionary ViewData { get; set; }
}
public class ControllerWithNonVisibleProperties
{
internal ActionContext ActionContext { get; set; }
public ActionBindingContext BindingContext { get; private set; }
}
private class ControllerWithAttributes
{
[ActionContext]

View File

@ -32,6 +32,23 @@ namespace Microsoft.AspNet.Mvc.ViewComponents
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 @@ namespace Microsoft.AspNet.Mvc.ViewComponents
throw new NotImplementedException();
}
}
private class VisibilityViewComponent : ViewComponent
{
[ViewComponentContext]
protected internal ViewComponentContext C { get; set; }
}
}
}
#endif