Fix #2278 - Only activate public properties
This commit is contained in:
parent
f5cabf2029
commit
a452b10ba4
|
|
@ -87,7 +87,8 @@ namespace Microsoft.AspNet.Mvc.Razor
|
||||||
PropertyActivators = PropertyActivator<ViewContext>.GetPropertiesToActivate(
|
PropertyActivators = PropertyActivator<ViewContext>.GetPropertiesToActivate(
|
||||||
type,
|
type,
|
||||||
typeof(RazorInjectAttribute),
|
typeof(RazorInjectAttribute),
|
||||||
CreateActivateInfo)
|
CreateActivateInfo,
|
||||||
|
includeNonPublic: true)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -166,6 +166,31 @@ namespace Microsoft.AspNet.Mvc.Core
|
||||||
Assert.Null(controller.ActionContext);
|
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]
|
[Fact]
|
||||||
public void CreateController_ThrowsIfPropertyCannotBeActivated()
|
public void CreateController_ThrowsIfPropertyCannotBeActivated()
|
||||||
{
|
{
|
||||||
|
|
@ -271,6 +296,13 @@ namespace Microsoft.AspNet.Mvc.Core
|
||||||
public ViewDataDictionary ViewData { get; set; }
|
public ViewDataDictionary ViewData { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class ControllerWithNonVisibleProperties
|
||||||
|
{
|
||||||
|
internal ActionContext ActionContext { get; set; }
|
||||||
|
|
||||||
|
public ActionBindingContext BindingContext { get; private set; }
|
||||||
|
}
|
||||||
|
|
||||||
private class ControllerWithAttributes
|
private class ControllerWithAttributes
|
||||||
{
|
{
|
||||||
[ActionContext]
|
[ActionContext]
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,23 @@ namespace Microsoft.AspNet.Mvc.ViewComponents
|
||||||
Assert.Same(context, instance.ViewComponentContext);
|
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
|
private class TestViewComponent : ViewComponent
|
||||||
{
|
{
|
||||||
public Task ExecuteAsync()
|
public Task ExecuteAsync()
|
||||||
|
|
@ -39,6 +56,12 @@ namespace Microsoft.AspNet.Mvc.ViewComponents
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private class VisibilityViewComponent : ViewComponent
|
||||||
|
{
|
||||||
|
[ViewComponentContext]
|
||||||
|
protected internal ViewComponentContext C { get; set; }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue