Update ModelBinderAttribute not throw exceptions from BinderType property setter
This commit is contained in:
parent
852d6402eb
commit
9daf6b48a1
|
|
@ -26,39 +26,17 @@ namespace Microsoft.AspNet.Mvc
|
||||||
Inherited = true)]
|
Inherited = true)]
|
||||||
public class ModelBinderAttribute : Attribute, IModelNameProvider, IBinderTypeProviderMetadata
|
public class ModelBinderAttribute : Attribute, IModelNameProvider, IBinderTypeProviderMetadata
|
||||||
{
|
{
|
||||||
private Type _binderType;
|
|
||||||
private BindingSource _bindingSource;
|
private BindingSource _bindingSource;
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public Type BinderType
|
public Type BinderType { get; set; }
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
return _binderType;
|
|
||||||
}
|
|
||||||
set
|
|
||||||
{
|
|
||||||
if (value != null)
|
|
||||||
{
|
|
||||||
if (!typeof(IModelBinder).IsAssignableFrom(value))
|
|
||||||
{
|
|
||||||
throw new InvalidOperationException(
|
|
||||||
Resources.FormatBinderType_MustBeIModelBinder(
|
|
||||||
value.FullName,
|
|
||||||
typeof(IModelBinder).FullName));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
_binderType = value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public BindingSource BindingSource
|
public BindingSource BindingSource
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
if (_bindingSource == null && _binderType != null)
|
if (_bindingSource == null && BinderType != null)
|
||||||
{
|
{
|
||||||
return BindingSource.Custom;
|
return BindingSource.Custom;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,23 +8,6 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
|
||||||
{
|
{
|
||||||
public class ModelBinderAttributeTest
|
public class ModelBinderAttributeTest
|
||||||
{
|
{
|
||||||
[Fact]
|
|
||||||
public void InvalidBinderType_Throws()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var attribute = new ModelBinderAttribute();
|
|
||||||
|
|
||||||
var expected =
|
|
||||||
$"The type 'System.String' must implement '{typeof(IModelBinder).FullName}' " +
|
|
||||||
"to be used as a model binder.";
|
|
||||||
|
|
||||||
// Act
|
|
||||||
var ex = Assert.Throws<InvalidOperationException>(() => { attribute.BinderType = typeof(string); });
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
Assert.Equal(expected, ex.Message);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void NoBinderType_NoBindingSource()
|
public void NoBinderType_NoBindingSource()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue