C# 在哪里可以找到ValueProvider的GetKeys()扩展方法?
我正在尝试使用我在论坛上找到的一些代码来获得更好的ModelBinderC# 在哪里可以找到ValueProvider的GetKeys()扩展方法?,c#,asp.net-mvc,C#,Asp.net Mvc,我正在尝试使用我在论坛上找到的一些代码来获得更好的ModelBinder public class BetterDefaultModelBinder : DefaultModelBinder { public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { if (bindingContext == null)
public class BetterDefaultModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
if (bindingContext == null)
{
throw new ArgumentNullException("bindingContext");
}
if (bindingContext.ValueProvider.GetKeys().All(IsRequiredRouteValue))
{
return null;
}
// Notes:
// 1) ContainsPrefix("") == true, for all value providers (even providers with no values)
// 2) ContainsPrefix(null) => ArgumentNullException
if (!bindingContext.ValueProvider.ContainsPrefix(bindingContext.ModelName))
{
if (string.IsNullOrEmpty(bindingContext.ModelName) || !bindingContext.FallbackToEmptyPrefix)
{
return null;
}
// We couldn't find any entry that began with the (non-empty) prefix.
// If this is the top-level element, fall back to the empty prefix.
bindingContext = new ModelBindingContext
{
ModelMetadata = bindingContext.ModelMetadata,
ModelState = bindingContext.ModelState,
PropertyFilter = bindingContext.PropertyFilter,
ValueProvider = bindingContext.ValueProvider
};
}
// Simple model = int, string, etc.; determined by calling TypeConverter.CanConvertFrom(typeof(string))
// or by seeing if a value in the request exactly matches the name of the model we're binding.
// Complex type = everything else.
ValueProviderResult vpResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
if (vpResult != null)
{
return BindSimpleModel(controllerContext, bindingContext, vpResult);
}
return bindingContext.ModelMetadata.IsComplexType ? BindComplexModel(controllerContext, bindingContext) : null;
}
private static bool IsRequiredRouteValue(string value)
{
return new[] { "area", "controller", "action" }.Any(s => value.Equals(s, StringComparison.OrdinalIgnoreCase));
}
}
在所有这些代码中都包含以下代码块:
if (bindingContext.ValueProvider.GetKeys().All(IsRequiredRouteValue))
{
return null;
}
这里有一个对方法GetKeys()
的调用。我不知道这个方法从哪里来,VisualStudio告诉我它不存在。我假设这是一种扩展方法,对吗
这只是我缺少的一个使用语句吗?或者,代码的作者可能创建了自己的GetKeys()扩展方法,但没有提到它吗?Property
ValueProvider
是typeofIValueProvider
,它只公开了两个方法containsRefix()
和GetValue()
,所以我认为它一定是一个扩展方法该死的,这就是我所怀疑的,或者可能是较旧的.NET实现。