1

WEBAPI 中的 IModelBinderProvider 等效于什么?我已经阅读了这篇文章,仍然不明白如何替换默认绑定或插入自己的绑定规则。

编辑:当前的“经典”MVC 模型绑定器提供程序(实现 IModelBinderProvider)。

public IModelBinder GetBinder(Type modelType)
        {
            Type binderType;

            lock (_syncLock)
            {
                // Check to see if a type was already bound.
                if (_binders.ContainsKey(modelType))
                    binderType = _binders[modelType];
                else
                {
                    // Check the assembly and look for a <name>ModelBinder
                    Assembly assembly = Assembly.GetExecutingAssembly();
                    Type[] types = assembly.GetTypes();

                    // Convention over configuration
                    binderType = types.FirstOrDefault(t => t.Name == modelType.Name + "ModelBinder");

                    if (binderType != null)
                        _binders[modelType] = binderType;
                }
            }

            if (binderType == null)
                return null;

            var binder = (IModelBinder) DependencyResolver.Current.GetService(binderType);
            return binder;
        }

所以你可以看到我正在尝试使用依赖解析器来选择活页夹。

同样在配置中,我将默认活页夹替换为

DependencyResolver.SetResolver(new StructureMapDependencyResolver(ObjectFactory.Container));

最后,如果我在示例“LocationID”中发送参数(作为表单或查询字符串)并且我有这样的 webapi 操作

public HttpResponseMessage MyAction(MyCls obj) {
   // do something
}

public class MyCls{
    public string LocationID {get;set;}
}

我需要绑定 MyCls 和 LocationID 来获取提供的“LocationID”参数的值。

4

0 回答 0