本文整理汇总了C#中Microsoft.AspNetCore.Mvc.Abstractions.ParameterDescriptor类的典型用法代码示例。如果您正苦于以下问题:C# ParameterDescriptor类的具体用法?C# ParameterDescriptor怎么用?C# ParameterDescriptor使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ParameterDescriptor类属于Microsoft.AspNetCore.Mvc.Abstractions命名空间,在下文中一共展示了ParameterDescriptor类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。
示例1: BindParameter_WithModelBinderType_NoData
public async Task BindParameter_WithModelBinderType_NoData()
{
// Arrange
var argumentBinder = ModelBindingTestHelper.GetArgumentBinder();
var parameter = new ParameterDescriptor()
{
Name = "Parameter1",
BindingInfo = new BindingInfo()
{
BinderType = typeof(NullModelNotSetModelBinder)
},
ParameterType = typeof(string)
};
// No data is passed.
var testContext = ModelBindingTestHelper.GetTestContext();
var modelState = testContext.ModelState;
// Act
var modelBindingResult = await argumentBinder.BindModelAsync(parameter, testContext);
// Assert
Assert.False(modelBindingResult.IsModelSet);
// ModelState (not set unless inner binder sets it)
Assert.True(modelState.IsValid);
Assert.Empty(modelState);
}
开发者ID:xuchrist,项目名称:Mvc,代码行数:29,代码来源:BinderTypeBasedModelBinderIntegrationTest.cs
示例2: ModelMetadataTypeAttribute_ValidBaseClass_NoModelStateErrors
public async Task ModelMetadataTypeAttribute_ValidBaseClass_NoModelStateErrors()
{
// Arrange
var input = "{ \"Name\": \"MVC\", \"Contact\":\"4258959019\", \"Category\":\"Technology\"," +
"\"CompanyName\":\"Microsoft\", \"Country\":\"USA\",\"Price\": 21, " +
"\"ProductDetails\": {\"Detail1\": \"d1\", \"Detail2\": \"d2\", \"Detail3\": \"d3\"}}";
var argumentBinder = ModelBindingTestHelper.GetArgumentBinder();
var parameter = new ParameterDescriptor()
{
Name = "Parameter1",
ParameterType = typeof(ProductViewModel),
BindingInfo = new BindingInfo()
{
BindingSource = BindingSource.Body
}
};
var testContext = ModelBindingTestHelper.GetTestContext(
request =>
{
request.Body = new MemoryStream(Encoding.UTF8.GetBytes(input));
request.ContentType = "application/json;charset=utf-8";
});
var modelState = testContext.ModelState;
// Act
var modelBindingResult = await argumentBinder.BindModelAsync(parameter, testContext);
// Assert
Assert.True(modelBindingResult.IsModelSet);
var boundPerson = Assert.IsType<ProductViewModel>(modelBindingResult.Model);
Assert.True(modelState.IsValid);
Assert.NotNull(boundPerson);
}
开发者ID:xuchrist,项目名称:Mvc,代码行数:35,代码来源:BodyValidationIntegrationTests.cs
示例3: GenericModelBinder_BindsCollection_ElementTypeFromGreedyModelBinder_EmptyPrefix_Success
public async Task GenericModelBinder_BindsCollection_ElementTypeFromGreedyModelBinder_EmptyPrefix_Success()
{
// Arrange
var argumentBinder = ModelBindingTestHelper.GetArgumentBinder();
var parameter = new ParameterDescriptor()
{
Name = "parameter",
ParameterType = typeof(List<IFormCollection>)
};
// Need to have a key here so that the GenericModelBinder will recurse to bind elements.
var testContext = ModelBindingTestHelper.GetTestContext(request =>
{
request.QueryString = new QueryString("?index=10");
});
var modelState = testContext.ModelState;
// Act
var modelBindingResult = await argumentBinder.BindModelAsync(parameter, testContext);
// Assert
Assert.True(modelBindingResult.IsModelSet);
var model = Assert.IsType<List<IFormCollection>>(modelBindingResult.Model);
var formCollection = Assert.Single(model);
Assert.NotNull(formCollection);
Assert.Equal(0, modelState.ErrorCount);
Assert.True(modelState.IsValid);
Assert.Empty(modelState);
}
开发者ID:xuchrist,项目名称:Mvc,代码行数:31,代码来源:GenericModelBinderIntegrationTest.cs
示例4: BindProperty_WithData_WithEmptyPrefix_GetsBound
public async Task BindProperty_WithData_WithEmptyPrefix_GetsBound()
{
// Arrange
var argumentBinder = ModelBindingTestHelper.GetArgumentBinder();
var parameter = new ParameterDescriptor()
{
Name = "Parameter1",
BindingInfo = new BindingInfo(),
ParameterType = typeof(Person)
};
var testContext = ModelBindingTestHelper.GetTestContext();
var modelState = testContext.ModelState;
// Act
var modelBindingResult = await argumentBinder.BindModelAsync(parameter, testContext);
// Assert
// ModelBindingResult
Assert.True(modelBindingResult.IsModelSet);
// Model
var boundPerson = Assert.IsType<Person>(modelBindingResult.Model);
Assert.NotNull(boundPerson);
Assert.NotNull(boundPerson.Token);
// ModelState
Assert.True(modelState.IsValid);
Assert.Equal(0, modelState.Count);
}
开发者ID:xuchrist,项目名称:Mvc,代码行数:31,代码来源:CancellationTokenModelBinderIntegrationTest.cs
示例5: BindParameter_NoData_DoesNotGetBound
public async Task BindParameter_NoData_DoesNotGetBound()
{
// Arrange
var argumentBinder = ModelBindingTestHelper.GetArgumentBinder();
var parameter = new ParameterDescriptor()
{
Name = "Parameter1",
BindingInfo = new BindingInfo()
{
BinderModelName = "CustomParameter",
},
ParameterType = typeof(byte[])
};
// No data is passed.
var operationContext = ModelBindingTestHelper.GetOperationBindingContext();
var modelState = operationContext.ActionContext.ModelState;
// Act
var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext) ?? default(ModelBindingResult);
// Assert
// ModelBindingResult
Assert.False(modelBindingResult.IsModelSet);
// ModelState
Assert.True(modelState.IsValid);
Assert.Empty(modelState.Keys);
}
开发者ID:cemalshukriev,项目名称:Mvc,代码行数:31,代码来源:ByteArrayModelBinderIntegrationTest.cs
示例6: BindParameterFromService_NoPrefix_GetsBound
public async Task BindParameterFromService_NoPrefix_GetsBound()
{
// Arrange
var argumentBinder = ModelBindingTestHelper.GetArgumentBinder();
var parameter = new ParameterDescriptor
{
Name = "ControllerProperty",
BindingInfo = new BindingInfo
{
BindingSource = BindingSource.Services,
},
// Use a service type already in defaults.
ParameterType = typeof(JsonOutputFormatter),
};
var operationContext = ModelBindingTestHelper.GetOperationBindingContext();
var modelState = operationContext.ActionContext.ModelState;
// Act
var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext) ?? default(ModelBindingResult);
// Assert
// ModelBindingResult
Assert.True(modelBindingResult.IsModelSet);
// Model
var outputFormatter = Assert.IsType<JsonOutputFormatter>(modelBindingResult.Model);
Assert.NotNull(outputFormatter);
// ModelState
Assert.True(modelState.IsValid);
Assert.Empty(modelState);
}
开发者ID:cemalshukriev,项目名称:Mvc,代码行数:34,代码来源:ServicesModelBinderIntegrationTest.cs
示例7: BindParameter_WithTypeProperty_IsNotBound
public async Task BindParameter_WithTypeProperty_IsNotBound()
{
// Arrange
var options = new MvcOptions();
var setup = new MvcCoreMvcOptionsSetup(new TestHttpRequestStreamReaderFactory());
// Adding a custom model binder for Type to ensure it doesn't get called
options.ModelBinderProviders.Insert(0, new TypeModelBinderProvider());
setup.Configure(options);
var argumentBinder = ModelBindingTestHelper.GetArgumentBinder(options);
var parameter = new ParameterDescriptor()
{
Name = "Parameter1",
BindingInfo = new BindingInfo(),
ParameterType = typeof(TypesBundle),
};
var testContext = ModelBindingTestHelper.GetTestContext(request =>
{
request.Form = new FormCollection(new Dictionary<string, StringValues>
{
{ "name", new[] { "Fred" } },
{ "type", new[] { "SomeType" } },
{ "typeArray", new[] { "SomeType1", "SomeType2" } },
{ "typeList", new[] { "SomeType1", "SomeType2" } },
{ "typeDictionary", new[] { "parameter[0].Key=key", "parameter[0].Value=value" } },
{ "methodInfo", new[] { "value" } },
{ "func", new[] { "value" } },
});
});
var modelState = testContext.ModelState;
// Act
var modelBindingResult = await argumentBinder.BindModelAsync(parameter, testContext);
// Assert
// ModelBindingResult
Assert.True(modelBindingResult.IsModelSet);
// Model
var boundPerson = Assert.IsType<TypesBundle>(modelBindingResult.Model);
Assert.NotNull(boundPerson);
Assert.Equal("Fred", boundPerson.Name);
// ModelState
// The TypeModelBinder should not be called
Assert.True(modelState.IsValid);
}
开发者ID:ymd1223,项目名称:Mvc,代码行数:52,代码来源:ExcludeBindingMetadataProviderIntegrationTest.cs
示例8: BindProperty_WithData_GetsBound
public async Task BindProperty_WithData_GetsBound(bool fallBackScenario)
{
// Arrange
var argumentBinder = ModelBindingTestHelper.GetArgumentBinder();
var parameter = new ParameterDescriptor()
{
Name = "Parameter1",
BindingInfo = new BindingInfo(),
ParameterType = typeof(Person)
};
var prefix = fallBackScenario ? string.Empty : "Parameter1";
var queryStringKey = fallBackScenario ? "Token" : prefix + "." + "Token";
// any valid base64 string
var expectedValue = new byte[] { 12, 13 };
var value = Convert.ToBase64String(expectedValue);
var operationContext = ModelBindingTestHelper.GetOperationBindingContext(
request =>
{
request.QueryString = QueryString.Create(queryStringKey, value);
});
var modelState = operationContext.ActionContext.ModelState;
// Act
var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext) ?? default(ModelBindingResult);
// Assert
// ModelBindingResult
Assert.True(modelBindingResult.IsModelSet);
// Model
var boundPerson = Assert.IsType<Person>(modelBindingResult.Model);
Assert.NotNull(boundPerson);
Assert.NotNull(boundPerson.Token);
Assert.Equal(expectedValue, boundPerson.Token);
// ModelState
Assert.True(modelState.IsValid);
var entry = Assert.Single(modelState);
Assert.Equal(queryStringKey, entry.Key);
Assert.Empty(entry.Value.Errors);
Assert.Equal(ModelValidationState.Valid, entry.Value.ValidationState);
Assert.Equal(value, entry.Value.AttemptedValue);
Assert.Equal(value, entry.Value.RawValue);
}
开发者ID:cemalshukriev,项目名称:Mvc,代码行数:48,代码来源:ByteArrayModelBinderIntegrationTest.cs
示例9: BindProperty_WithData_WithEmptyPrefix_GetsBound
public async Task BindProperty_WithData_WithEmptyPrefix_GetsBound()
{
// Arrange
var argumentBinder = ModelBindingTestHelper.GetArgumentBinder();
var parameter = new ParameterDescriptor()
{
Name = "Parameter1",
BindingInfo = new BindingInfo(),
ParameterType = typeof(Person)
};
var data = "Some Data Is Better Than No Data.";
var testContext = ModelBindingTestHelper.GetTestContext(
request =>
{
request.QueryString = QueryString.Create("Address.Zip", "12345");
UpdateRequest(request, data, "Address.File");
});
var modelState = testContext.ModelState;
// Act
var modelBindingResult = await argumentBinder.BindModelAsync(parameter, testContext);
// Assert
// ModelBindingResult
Assert.True(modelBindingResult.IsModelSet);
// Model
var boundPerson = Assert.IsType<Person>(modelBindingResult.Model);
Assert.NotNull(boundPerson.Address);
var file = Assert.IsAssignableFrom<IFormFile>(boundPerson.Address.File);
Assert.Equal("form-data; name=Address.File; filename=text.txt", file.ContentDisposition);
var reader = new StreamReader(boundPerson.Address.File.OpenReadStream());
Assert.Equal(data, reader.ReadToEnd());
// ModelState
Assert.True(modelState.IsValid);
Assert.Equal(2, modelState.Count);
Assert.Single(modelState.Keys, k => k == "Address.Zip");
var key = Assert.Single(modelState.Keys, k => k == "Address.File");
Assert.Null(modelState[key].RawValue);
Assert.Empty(modelState[key].Errors);
Assert.Equal(ModelValidationState.Valid, modelState[key].ValidationState);
}
开发者ID:ymd1223,项目名称:Mvc,代码行数:46,代码来源:FormFileModelBindingIntegrationTest.cs
示例10: BindProperty_WithData_WithPrefix_GetsBound
public async Task BindProperty_WithData_WithPrefix_GetsBound()
{
// Arrange
var argumentBinder = ModelBindingTestHelper.GetArgumentBinder();
var parameter = new ParameterDescriptor()
{
Name = "Parameter1",
BindingInfo = new BindingInfo()
{
BinderModelName = "CustomParameter",
},
ParameterType = typeof(Person)
};
var testContext = ModelBindingTestHelper.GetTestContext(request =>
{
request.QueryString = QueryString.Create("CustomParameter.Address.Zip", "1");
});
var modelState = testContext.ModelState;
// Act
var modelBindingResult = await argumentBinder.BindModelAsync(parameter, testContext);
// Assert
// ModelBindingResult
Assert.True(modelBindingResult.IsModelSet);
// Model
var boundPerson = Assert.IsType<Person>(modelBindingResult.Model);
Assert.NotNull(boundPerson);
Assert.NotNull(boundPerson.Address);
Assert.Equal(1, boundPerson.Address.Zip);
// ModelState
Assert.True(modelState.IsValid);
Assert.Equal(1, modelState.Keys.Count());
var key = Assert.Single(modelState.Keys, k => k == "CustomParameter.Address.Zip");
Assert.Equal("1", modelState[key].AttemptedValue);
Assert.Equal("1", modelState[key].RawValue);
Assert.Empty(modelState[key].Errors);
Assert.Equal(ModelValidationState.Valid, modelState[key].ValidationState);
}
开发者ID:xuchrist,项目名称:Mvc,代码行数:46,代码来源:SimpleTypeModelBinderIntegrationTest.cs
示例11: ComplexModel_PrefixSelected_ByValueProviderValue_WithFilteredValueProviders
public async Task ComplexModel_PrefixSelected_ByValueProviderValue_WithFilteredValueProviders()
{
// Arrange
var argumentBinder = ModelBindingTestHelper.GetArgumentBinder();
var parameter = new ParameterDescriptor()
{
Name = "parameter",
ParameterType = typeof(Person2),
BindingInfo = new BindingInfo()
{
BindingSource = BindingSource.Query,
},
};
var operationContext = ModelBindingTestHelper.GetOperationBindingContext(request =>
{
// This will cause selection of the "parameter" prefix.
request.QueryString = new QueryString("?parameter=");
// This value won't be used, because we select the "parameter" prefix.
request.Form = new FormCollection(new Dictionary<string, StringValues>()
{
{ "Name", "Billy" },
});
});
var modelState = operationContext.ActionContext.ModelState;
// Act
var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext) ?? default(ModelBindingResult);
// Assert
Assert.True(modelBindingResult.IsModelSet);
Assert.Equal("parameter", modelBindingResult.Key);
var model = Assert.IsType<Person2>(modelBindingResult.Model);
Assert.Null(model.Name);
Assert.Equal(0, modelState.Count);
Assert.Equal(0, modelState.ErrorCount);
Assert.True(modelState.IsValid);
}
开发者ID:cemalshukriev,项目名称:Mvc,代码行数:42,代码来源:ModelPrefixSelectionIntegrationTest.cs
示例12: ArrayModelBinder_BindsArrayOfSimpleType_WithExplicitPrefix_Success
public async Task ArrayModelBinder_BindsArrayOfSimpleType_WithExplicitPrefix_Success()
{
// Arrange
var argumentBinder = ModelBindingTestHelper.GetArgumentBinder();
var parameter = new ParameterDescriptor()
{
Name = "parameter",
BindingInfo = new BindingInfo()
{
BinderModelName = "prefix",
},
ParameterType = typeof(int[])
};
var testContext = ModelBindingTestHelper.GetTestContext(request =>
{
request.QueryString = new QueryString("?prefix[0]=10&prefix[1]=11");
});
var modelState = testContext.ModelState;
// Act
var modelBindingResult = await argumentBinder.BindModelAsync(parameter, testContext);
// Assert
Assert.True(modelBindingResult.IsModelSet);
var model = Assert.IsType<int[]>(modelBindingResult.Model);
Assert.Equal(new int[] { 10, 11 }, model);
Assert.Equal(2, modelState.Count);
Assert.Equal(0, modelState.ErrorCount);
Assert.True(modelState.IsValid);
var entry = Assert.Single(modelState, kvp => kvp.Key == "prefix[0]").Value;
Assert.Equal("10", entry.AttemptedValue);
Assert.Equal("10", entry.RawValue);
entry = Assert.Single(modelState, kvp => kvp.Key == "prefix[1]").Value;
Assert.Equal("11", entry.AttemptedValue);
Assert.Equal("11", entry.RawValue);
}
开发者ID:ymd1223,项目名称:Mvc,代码行数:42,代码来源:ArrayModelBinderIntegrationTest.cs
示例13: BindPropertyFromHeader_WithPrefix_GetsBound
public async Task BindPropertyFromHeader_WithPrefix_GetsBound()
{
// Arrange
var argumentBinder = ModelBindingTestHelper.GetArgumentBinder();
var parameter = new ParameterDescriptor()
{
Name = "Parameter1",
BindingInfo = new BindingInfo()
{
BinderModelName = "prefix",
},
ParameterType = typeof(Person)
};
var testContext = ModelBindingTestHelper.GetTestContext(
request => request.Headers.Add("Header", new[] { "someValue" }));
var modelState = testContext.ModelState;
// Act
var modelBindingResult = await argumentBinder.BindModelAsync(parameter, testContext);
// Assert
// ModelBindingResult
Assert.True(modelBindingResult.IsModelSet);
// Model
var boundPerson = Assert.IsType<Person>(modelBindingResult.Model);
Assert.NotNull(boundPerson);
Assert.NotNull(boundPerson.Address);
Assert.Equal("someValue", boundPerson.Address.Street);
// ModelState
Assert.True(modelState.IsValid);
var entry = Assert.Single(modelState);
Assert.Equal("prefix.Address.Header", entry.Key);
Assert.Empty(entry.Value.Errors);
Assert.Equal(ModelValidationState.Valid, entry.Value.ValidationState);
Assert.Equal("someValue", entry.Value.AttemptedValue);
Assert.Equal(new string[] { "someValue" }, entry.Value.RawValue);
}
开发者ID:xuchrist,项目名称:Mvc,代码行数:41,代码来源:HeaderModelBinderIntegrationTest.cs
示例14: MutableObjectModelBinder_BindsNestedPOCO_WithBodyModelBinder_WithPrefix_Success
public async Task MutableObjectModelBinder_BindsNestedPOCO_WithBodyModelBinder_WithPrefix_Success()
{
// Arrange
var argumentBinder = ModelBindingTestHelper.GetArgumentBinder();
var parameter = new ParameterDescriptor()
{
Name = "parameter",
ParameterType = typeof(Order1)
};
// Need to have a key here so that the MutableObjectModelBinder will recurse to bind elements.
var operationContext = ModelBindingTestHelper.GetOperationBindingContext(request =>
{
request.QueryString = new QueryString("?parameter.Customer.Name=bill");
SetJsonBodyContent(request, AddressBodyContent);
});
var modelState = operationContext.ActionContext.ModelState;
// Act
var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext) ?? default(ModelBindingResult);
// Assert
Assert.True(modelBindingResult.IsModelSet);
var model = Assert.IsType<Order1>(modelBindingResult.Model);
Assert.NotNull(model.Customer);
Assert.Equal("bill", model.Customer.Name);
Assert.NotNull(model.Customer.Address);
Assert.Equal(AddressStreetContent, model.Customer.Address.Street);
Assert.Equal(1, modelState.Count);
Assert.Equal(0, modelState.ErrorCount);
Assert.True(modelState.IsValid);
var entry = Assert.Single(modelState, e => e.Key == "parameter.Customer.Name").Value;
Assert.Equal("bill", entry.AttemptedValue);
Assert.Equal("bill", entry.RawValue);
}
开发者ID:cemalshukriev,项目名称:Mvc,代码行数:39,代码来源:MutableObjectModelBinderIntegrationTest.cs
示例15: ModelMetadataType_ValidArray_NoModelStateErrors
public async Task ModelMetadataType_ValidArray_NoModelStateErrors()
{
// Arrange
var input = "[" +
"{ \"Name\": \"MVC\", \"Contact\":\"4258959019\", \"Category\":\"Technology\"," +
"\"CompanyName\":\"Microsoft\", \"Country\":\"USA\",\"Price\": 21, " +
"\"ProductDetails\": {\"Detail1\": \"d1\", \"Detail2\": \"d2\", \"Detail3\": \"d3\"}}," +
"{ \"Name\": \"MVC too\", \"Contact\":\"4258959020\", \"Category\":\"Technology\"," +
"\"CompanyName\":\"Microsoft\", \"Country\":\"USA\",\"Price\": 22, " +
"\"ProductDetails\": {\"Detail1\": \"d2\", \"Detail2\": \"d3\", \"Detail3\": \"d4\"}}" +
"]";
var argumentBinding = ModelBindingTestHelper.GetArgumentBinder();
var parameter = new ParameterDescriptor
{
Name = "Parameter1",
ParameterType = typeof(IEnumerable<ProductViewModel>),
BindingInfo = new BindingInfo
{
BindingSource = BindingSource.Body,
},
};
var testContext = ModelBindingTestHelper.GetTestContext(request =>
{
request.Body = new MemoryStream(Encoding.UTF8.GetBytes(input));
request.ContentType = "application/json;charset=utf-8";
});
var modelState = testContext.ModelState;
// Act
var result = await argumentBinding.BindModelAsync(parameter, testContext);
// Assert
Assert.True(modelState.IsValid);
Assert.True(result.IsModelSet);
var products = Assert.IsAssignableFrom<IEnumerable<ProductViewModel>>(result.Model);
Assert.Equal(2, products.Count());
}
开发者ID:xuchrist,项目名称:Mvc,代码行数:38,代码来源:BodyValidationIntegrationTests.cs
示例16: BindPropertyFromHeader_NoData_UsesFullPathAsKeyForModelStateErrors
public async Task BindPropertyFromHeader_NoData_UsesFullPathAsKeyForModelStateErrors()
{
// Arrange
var argumentBinder = ModelBindingTestHelper.GetArgumentBinder();
var parameter = new ParameterDescriptor()
{
Name = "Parameter1",
BindingInfo = new BindingInfo()
{
BinderModelName = "CustomParameter",
},
ParameterType = typeof(Person)
};
// Do not add any headers.
var testContext = ModelBindingTestHelper.GetTestContext();
var modelState = testContext.ModelState;
// Act
var modelBindingResult = await argumentBinder.BindModelAsync(parameter, testContext);
// Assert
// ModelBindingResult
Assert.True(modelBindingResult.IsModelSet);
// Model
var boundPerson = Assert.IsType<Person>(modelBindingResult.Model);
Assert.NotNull(boundPerson);
// ModelState
Assert.False(modelState.IsValid);
var key = Assert.Single(modelState.Keys);
Assert.Equal("CustomParameter.Address.Header", key);
var error = Assert.Single(modelState[key].Errors);
// Mono issue - https://github.com/aspnet/External/issues/19
Assert.Equal(PlatformNormalizer.NormalizeContent("The Street field is required."), error.ErrorMessage);
}
开发者ID:xuchrist,项目名称:Mvc,代码行数:38,代码来源:HeaderModelBinderIntegrationTest.cs
示例17: BindProperty_WithData_WithPrefix_GetsBound
public async Task BindProperty_WithData_WithPrefix_GetsBound()
{
// Arrange
var argumentBinder = ModelBindingTestHelper.GetArgumentBinder();
var parameter = new ParameterDescriptor()
{
Name = "Parameter1",
BindingInfo = new BindingInfo()
{
BinderModelName = "CustomParameter",
},
ParameterType = typeof(Person)
};
var operationContext = ModelBindingTestHelper.GetOperationBindingContext();
var modelState = operationContext.ActionContext.ModelState;
// Act
var modelBindingResult = await argumentBinder.BindModelAsync(parameter, operationContext) ?? default(ModelBindingResult);
// Assert
// ModelBindingResult
Assert.True(modelBindingResult.IsModelSet);
// Model
var boundPerson = Assert.IsType<Person>(modelBindingResult.Model);
Assert.NotNull(boundPerson);
Assert.NotNull(boundPerson.Token);
// ModelState
Assert.True(modelState.IsValid);
Assert.Empty(modelState.Keys);
}
开发者ID:cemalshukriev,项目名称:Mvc,代码行数:36,代码来源:CancellationTokenModelBinderIntegrationTest.cs
示例18: BindParameterFromService_WithData_GetsBound
public async Task BindParameterFromService_WithData_GetsBound()
{
// Arrange
var argumentBinder = ModelBindingTestHelper.GetArgumentBinder();
var parameter = new ParameterDescriptor()
{
Name = "Parameter1",
BindingInfo = new BindingInfo()
{
BinderModelName = "CustomParameter",
BindingSource = BindingSource.Services
},
// Using a service type already in defaults.
ParameterType = typeof(JsonOutputFormatter)
};
var testContext = ModelBindingTestHelper.GetTestContext();
var modelState = testContext.ModelState;
// Act
var modelBindingResult = await argumentBinder.BindModelAsync(parameter, testContext);
// Assert
// ModelBindingResult
Assert.True(modelBindingResult.IsModelSet);
// Model
var outputFormatter = Assert.IsType<JsonOutputFormatter>(modelBindingResult.Model);
Assert.NotNull(outputFormatter);
// ModelState
Assert.True(modelState.IsValid);
Assert.Empty(modelState.Keys);
}
开发者ID:xuchrist,项目名称:Mvc,代码行数:36,代码来源:ServicesModelBinderIntegrationTest.cs
示例19: BindParameter_NoData_DoesNotGetBound
public async Task BindParameter_NoData_DoesNotGetBound()
{
// Arrange
var argumentBinder = ModelBindingTestHelper.GetArgumentBinder();
var parameter = new ParameterDescriptor()
{
Name = "Parameter1",
BindingInfo = new BindingInfo()
{
BinderModelName = "CustomParameter",
},
ParameterType = typeof(IFormFile)
};
// No data is passed.
var testContext = ModelBindingTestHelper.GetTestContext(
request => UpdateRequest(request, data: null, name: null));
var modelState = testContext.ModelState;
// Act
var modelBindingResult = await argumentBinder.BindModelAsync(parameter, testContext);
// Assert
Assert.False(modelBindingResult.IsModelSet);
// ModelState
Assert.True(modelState.IsValid);
Assert.Empty(modelState.Keys);
}
开发者ID:ymd1223,项目名称:Mvc,代码行数:31,代码来源:FormFileModelBindingIntegrationTest.cs
示例20: BindParameter_WithData_GetsBound
public async Task BindParameter_WithData_GetsBound()
{
// Arrange
var argumentBinder = ModelBindingTestHelper.GetArgumentBinder();
var parameter = new ParameterDescriptor
{
Name = "Parameter1",
BindingInfo = new BindingInfo
{
// Setting a custom parameter prevents it from falling back to an empty prefix.
BinderModelName = "CustomParameter",
},
ParameterType = typeof(IFormFile)
};
var data = "Some Data Is Better Than No Data.";
var testContext = ModelBindingTestHelper.GetTestContext(
request =>
{
UpdateRequest(request, data, "CustomParameter");
});
var modelState = testContext.ModelState;
// Act
var modelBindingResult = await argumentBinder.BindModelAsync(parameter, testContext);
// Assert
// ModelBindingResult
Assert.True(modelBindingResult.IsModelSet);
// Model
var file = Assert.IsType<FormFile>(modelBindingResult.Model);
Assert.NotNull(file);
Assert.Equal("form-data; name=CustomParameter; filename=text.txt", file.ContentDisposition);
var reader = new StreamReader(file.OpenReadStream());
Assert.Equal(data, reader.ReadToEnd());
// ModelState
Assert.True(modelState.IsValid);
var entry = Assert.Single(modelState);
Assert.Equal("CustomParameter", entry.Key);
Assert.Empty(entry.Value.Errors);
Assert.Equal(ModelValidationState.Valid, entry.Value.ValidationState);
Assert.Null(entry.Value.AttemptedValue);
Assert.Null(entry.Value.RawValue);
}
开发者ID:ymd1223,项目名称:Mvc,代码行数:47,代码来源:FormFileModelBindingIntegrationTest.cs
注:本文中的Microsoft.AspNetCore.Mvc.Abstractions.ParameterDescriptor类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论