如何在Controller Action上放置自定义模型绑定器

时间:2016-10-28 17:26:27

标签: asp.net-core asp.net-core-mvc model-binding asp.net-core-1.0

我有一个动作,我想放置一个自定义模型绑定器。我不想要全局粘合剂粘合剂。我只想使用[ModelBinder(...)]指定的动作上的活页夹

当我使用下面的代码时,我的Binder Never被调用,我的动作也没有。

{{2在Microsoft.Extensions..Internal.ActivatorUtilities.GetService(IServiceProvider sp,Type type,Type requiredBy,Boolean isDefaultParameterRequired)中的堆栈跟踪:

   在lambda_method(Closure,IServiceProvider,Object [])    在Microsoft.AspNetCore.Mvc.ModelBinding.Binders.BinderTypeModelBinder.d__2.MoveNext() ---抛出异常的前一个位置的堆栈跟踪结束---

看起来我必须在某个地方注册这个绑定器?

1 个答案:

答案 0 :(得分:1)

尝试删除Name属性上的ModelBinder属性,并且不要忘记使用下一个代码在binder中设置实际结果值:

bindingContext.Result = ModelBindingResult.Success(resultModel);