ASP.NET Core升级后表单字典模型绑定异常适配方案咨询
问题背景
一段业务代码在ASP.NET MVC 5环境中可稳定运行,升级至ASP.NET Core后出现功能异常,提交的表单数据格式如下图所示:
相关代码定义
接口端点定义
ASP.NET Core中的接口端点代码如下:
public IActionResult GetExpressionEditor([FromForm]GetExpressionEditorWidgetModel model)
模型类定义
接口绑定使用的C#模型类代码如下:
public class GetExpressionEditorWidgetModel { public string Name { get; set; } public ExpressionEditorViewModel ExpressionEditorData { get; set; } } public class ExpressionEditorViewModel { public string Expression { get; set; } public IDictionary<string, ExpressionEditorAttribute> SymbolToAttributeMap { get { if (this._symbolToAttributeMap == null) { return new Dictionary<string, ExpressionEditorAttribute>(); } return this._symbolToAttributeMap.ToDictionary(entry => entry.Key, entry => ExpressionEditorAttribute.SetDefaults(entry.Key, entry.Value)); } // 默认setter set { this._symbolToAttributeMap = value; } } public Dictionary<string, string> Data { get; set; } private IDictionary<string, ExpressionEditorAttribute> _symbolToAttributeMap; }
异常信息
模型绑定过程中抛出如下异常:
System.ArgumentNullException
错误信息:Value cannot be null. Parameter name: keyat System.ThrowHelper.ThrowArgumentNullException(ExceptionArgument argument) at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add) at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer) at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector) at Microsoft.AspNetCore.Mvc.ModelBinding.Binders.CollectionModelBinder`1.<BindModelAsync>d__14.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Mvc.ModelBinding.Binders.DictionaryModelBinder`2.d__7.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Microsoft.AspNetCore.Mvc.ModelBinding.Binders.ComplexTypeModelBinder.d__10.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Microsoft.AspNetCore.Mvc.ModelBinding.Binders.ComplexTypeModelBinder.d__10.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Mvc.ModelBinding.ParameterBinder.d__11.MoveNext()
排查结论与待咨询问题
经数小时调试、跟踪模型绑定内部执行逻辑,确认ASP.NET Core默认不支持expressionEditorData.data.index这种.key后缀的Data字典表单语法,仅支持如下索引式字典提交语法:
expressionEditorData.data[0].Key = "index" expressionEditorData.data[0].Value = 0
待咨询的问题如下:
- 是否有内置配置可实现
.key = value格式的字典绑定? - 是否有其他可直接使用的内置模型绑定器?
- 若要保留原有提交格式的绑定能力,是否只能通过自定义模型绑定实现?
内容的提问来源于stack exchange,提问作者badazzhindu
相关产品推荐
相关产品推荐

