哪里可以找到ValueProvider的GetKeys()扩展方法?

时间:2015-06-05 02:51:50

标签: c# asp.net-mvc

我试图使用我在论坛上找到的代码来获得更好的ModelBinder

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()。我无法弄清楚这种方法的来源,Visual Studio告诉我它并不存在。假设这是一种扩展方法,我是否正确?

这只是一个我失踪的使用陈述吗?或者代码的作者是否可能创建了自己的GetKeys()扩展方法并且未提及它?

0 个答案:

没有答案