我在 Web Api 控制器中有这个动作:
public Data GetData(ComplexModel model)
模型是
class ComplexModel
{
Guid Guid1 { get; set;}
Guid Guid2 { get; set;}
string String1 { get; set;}
}
我想为 Guid 类型指定自定义绑定器,例如空字符串或 null 将绑定到空 guid 并且不想使用可为空的类型。试图像这样注册模型活页夹:
var pb = configuration.ParameterBindingRules;
pb.Insert(0, typeof(Guid), param => param.BindWithModelBinding(new GuidBinder()));
但它没有被调用,我得到无效的模型状态,错误消息是空字符串不能转换为类型 Guid。