parent
1b7d52c84c
commit
822d84a2b4
|
|
@ -261,7 +261,7 @@ namespace Microsoft.AspNet.Mvc
|
|||
await InvokeActionMethodFilter();
|
||||
}
|
||||
|
||||
private async Task<IDictionary<string, object>> GetActionArguments(ModelStateDictionary modelState)
|
||||
internal async Task<IDictionary<string, object>> GetActionArguments(ModelStateDictionary modelState)
|
||||
{
|
||||
var actionBindingContext = await _bindingProvider.GetActionBindingContextAsync(_actionContext);
|
||||
var parameters = _descriptor.Parameters;
|
||||
|
|
@ -304,8 +304,10 @@ namespace Microsoft.AspNet.Mvc
|
|||
HttpContext = actionBindingContext.ActionContext.HttpContext,
|
||||
FallbackToEmptyPrefix = true
|
||||
};
|
||||
await actionBindingContext.ModelBinder.BindModelAsync(modelBindingContext);
|
||||
parameterValues[parameter.Name] = modelBindingContext.Model;
|
||||
if (await actionBindingContext.ModelBinder.BindModelAsync(modelBindingContext))
|
||||
{
|
||||
parameterValues[parameter.Name] = modelBindingContext.Model;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -5,8 +5,10 @@ using System;
|
|||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNet.Http;
|
||||
using Microsoft.AspNet.Mvc.ModelBinding;
|
||||
using Microsoft.AspNet.Routing;
|
||||
using Microsoft.AspNet.Testing;
|
||||
using Microsoft.Framework.DependencyInjection;
|
||||
|
|
@ -1319,6 +1321,168 @@ namespace Microsoft.AspNet.Mvc
|
|||
return invoker;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetActionArguments_DoesNotAddActionArgumentsToModelStateDictionary_IfBinderReturnsFalse()
|
||||
{
|
||||
// Arrange
|
||||
Func<object, int> method = x => 1;
|
||||
var actionDescriptor = new ReflectedActionDescriptor
|
||||
{
|
||||
MethodInfo = method.Method,
|
||||
Parameters = new List<ParameterDescriptor>
|
||||
{
|
||||
new ParameterDescriptor
|
||||
{
|
||||
Name = "foo",
|
||||
ParameterBindingInfo = new ParameterBindingInfo("foo", typeof(object))
|
||||
}
|
||||
}
|
||||
};
|
||||
var binder = new Mock<IModelBinder>();
|
||||
binder.Setup(b => b.BindModelAsync(It.IsAny<ModelBindingContext>()))
|
||||
.Returns(Task.FromResult(result: false));
|
||||
var actionContext = new ActionContext(new RouteContext(Mock.Of<HttpContext>()),
|
||||
actionDescriptor);
|
||||
var bindingContext = new ActionBindingContext(actionContext,
|
||||
Mock.Of<IModelMetadataProvider>(),
|
||||
binder.Object,
|
||||
Mock.Of<IValueProvider>(),
|
||||
Mock.Of<IInputFormatterProvider>(),
|
||||
Enumerable.Empty<IModelValidatorProvider>());
|
||||
|
||||
var actionBindingContextProvider = new Mock<IActionBindingContextProvider>();
|
||||
actionBindingContextProvider.Setup(p => p.GetActionBindingContextAsync(It.IsAny<ActionContext>()))
|
||||
.Returns(Task.FromResult(bindingContext));
|
||||
|
||||
var invoker = new ReflectedActionInvoker(actionContext,
|
||||
actionDescriptor,
|
||||
Mock.Of<IControllerFactory>(),
|
||||
actionBindingContextProvider.Object,
|
||||
Mock.Of<INestedProviderManager<FilterProviderContext>>());
|
||||
|
||||
var modelStateDictionary = new ModelStateDictionary();
|
||||
|
||||
// Act
|
||||
var result = await invoker.GetActionArguments(modelStateDictionary);
|
||||
|
||||
// Assert
|
||||
Assert.Empty(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetActionArguments_AddsActionArgumentsToModelStateDictionary_IfBinderReturnsTrue()
|
||||
{
|
||||
// Arrange
|
||||
Func<object, int> method = x => 1;
|
||||
var actionDescriptor = new ReflectedActionDescriptor
|
||||
{
|
||||
MethodInfo = method.Method,
|
||||
Parameters = new List<ParameterDescriptor>
|
||||
{
|
||||
new ParameterDescriptor
|
||||
{
|
||||
Name = "foo",
|
||||
ParameterBindingInfo = new ParameterBindingInfo("foo", typeof(object))
|
||||
}
|
||||
}
|
||||
};
|
||||
var value = "Hello world";
|
||||
var binder = new Mock<IModelBinder>();
|
||||
var metadataProvider = new EmptyModelMetadataProvider();
|
||||
binder.Setup(b => b.BindModelAsync(It.IsAny<ModelBindingContext>()))
|
||||
.Callback((ModelBindingContext context) =>
|
||||
{
|
||||
context.ModelMetadata = metadataProvider.GetMetadataForType(modelAccessor: null,
|
||||
modelType: typeof(string));
|
||||
context.Model = value;
|
||||
})
|
||||
.Returns(Task.FromResult(result: true));
|
||||
var actionContext = new ActionContext(new RouteContext(Mock.Of<HttpContext>()),
|
||||
actionDescriptor);
|
||||
var bindingContext = new ActionBindingContext(actionContext,
|
||||
Mock.Of<IModelMetadataProvider>(),
|
||||
binder.Object,
|
||||
Mock.Of<IValueProvider>(),
|
||||
Mock.Of<IInputFormatterProvider>(),
|
||||
Enumerable.Empty<IModelValidatorProvider>());
|
||||
|
||||
var actionBindingContextProvider = new Mock<IActionBindingContextProvider>();
|
||||
actionBindingContextProvider.Setup(p => p.GetActionBindingContextAsync(It.IsAny<ActionContext>()))
|
||||
.Returns(Task.FromResult(bindingContext));
|
||||
|
||||
var invoker = new ReflectedActionInvoker(actionContext,
|
||||
actionDescriptor,
|
||||
Mock.Of<IControllerFactory>(),
|
||||
actionBindingContextProvider.Object,
|
||||
Mock.Of<INestedProviderManager<FilterProviderContext>>());
|
||||
|
||||
var modelStateDictionary = new ModelStateDictionary();
|
||||
|
||||
// Act
|
||||
var result = await invoker.GetActionArguments(modelStateDictionary);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(1, result.Count);
|
||||
Assert.Equal(value, result["foo"]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Invoke_UsesDefaultValuesIfNotBound()
|
||||
{
|
||||
// Arrange
|
||||
var actionDescriptor = new ReflectedActionDescriptor
|
||||
{
|
||||
MethodInfo = typeof(TestController).GetTypeInfo()
|
||||
.DeclaredMethods
|
||||
.First(m => m.Name.Equals("ActionMethodWithDefaultValues", StringComparison.Ordinal)),
|
||||
Parameters = new List<ParameterDescriptor>
|
||||
{
|
||||
new ParameterDescriptor
|
||||
{
|
||||
Name = "value",
|
||||
ParameterBindingInfo = new ParameterBindingInfo("value", typeof(int))
|
||||
}
|
||||
},
|
||||
FilterDescriptors = new List<FilterDescriptor>()
|
||||
};
|
||||
|
||||
var binder = new Mock<IModelBinder>();
|
||||
var metadataProvider = new EmptyModelMetadataProvider();
|
||||
binder.Setup(b => b.BindModelAsync(It.IsAny<ModelBindingContext>()))
|
||||
.Returns(Task.FromResult(result: false));
|
||||
var context = new Mock<HttpContext>();
|
||||
context.SetupGet(c => c.Items)
|
||||
.Returns(new Dictionary<object, object>());
|
||||
var routeContext = new RouteContext(context.Object);
|
||||
var actionContext = new ActionContext(routeContext,
|
||||
actionDescriptor);
|
||||
var bindingContext = new ActionBindingContext(actionContext,
|
||||
Mock.Of<IModelMetadataProvider>(),
|
||||
binder.Object,
|
||||
Mock.Of<IValueProvider>(),
|
||||
Mock.Of<IInputFormatterProvider>(),
|
||||
Enumerable.Empty<IModelValidatorProvider>());
|
||||
|
||||
var actionBindingContextProvider = new Mock<IActionBindingContextProvider>();
|
||||
actionBindingContextProvider.Setup(p => p.GetActionBindingContextAsync(It.IsAny<ActionContext>()))
|
||||
.Returns(Task.FromResult(bindingContext));
|
||||
var controllerFactory = new Mock<IControllerFactory>();
|
||||
controllerFactory.Setup(c => c.CreateController(It.IsAny<ActionContext>()))
|
||||
.Returns(new TestController());
|
||||
|
||||
var invoker = new ReflectedActionInvoker(actionContext,
|
||||
actionDescriptor,
|
||||
controllerFactory.Object,
|
||||
actionBindingContextProvider.Object,
|
||||
Mock.Of<INestedProviderManager<FilterProviderContext>>());
|
||||
|
||||
// Act
|
||||
await invoker.InvokeActionAsync();
|
||||
|
||||
// Assert
|
||||
Assert.Equal(5, context.Object.Items["Result"]);
|
||||
}
|
||||
|
||||
public JsonResult ActionMethod()
|
||||
{
|
||||
return _result;
|
||||
|
|
@ -1328,5 +1492,24 @@ namespace Microsoft.AspNet.Mvc
|
|||
{
|
||||
throw _actionException;
|
||||
}
|
||||
|
||||
private sealed class TestController
|
||||
{
|
||||
public IActionResult ActionMethodWithDefaultValues(int value = 5)
|
||||
{
|
||||
return new TestActionResult { Value = value };
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class TestActionResult : IActionResult
|
||||
{
|
||||
public int Value { get; set; }
|
||||
|
||||
public Task ExecuteResultAsync(ActionContext context)
|
||||
{
|
||||
context.HttpContext.Items["Result"] = Value;
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,6 +30,7 @@
|
|||
<ItemGroup>
|
||||
<Compile Include="ActionDescriptorCreationCounter.cs" />
|
||||
<Compile Include="Controllers\HomeController.cs" />
|
||||
<Compile Include="Controllers\MonitorController.cs" />
|
||||
<Compile Include="Startup.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
|
|
|
|||
Loading…
Reference in New Issue