【发布时间】:2015-11-21 04:09:53
【问题描述】:
我有一个简单的文本绑定器来修改我的 web api 项目中的字符串。此绑定器应更改所有模型中的所有字符串。
public class TextoBinder : IModelBinder
{
public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
{
ValueProviderResult result = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
if (result == null)
return false;
var value = result.AttemptedValue.Trim();
// value changes here
bindingContext.Model = value;
return true;
}
}
此 TextoBinder 类已注册为服务:
config.Services.Insert(typeof(ModelBinderProvider), 0, new SimpleModelBinderProvider(typeof(string), new TextoBinder()));
问题在于 Web Api 在绑定输入模型时不会调用此绑定器,就像 MVC 在类似应用程序中所做的那样。我知道在 MVC 和 Web Api 中处理传入数据的方式不同,但我无法使文本绑定器适用于输入。
【问题讨论】:
标签: asp.net-web-api2 model-binding