我们在ASP.NET MVC 5中运行了一段运行良好的代码,但是一旦升级到ASP.NET核心,它就停止了工作。公布的数据如下:
端点(在ASP.NET核心中):
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));
}
// Default setter
set { this._symbolToAttributeMap = value; }
}
public Dictionary<string, string> Data { get; set; }
private IDictionary<string, ExpressionEditorAttribute> _symbolToAttributeMap;
}
我在模型绑定中遇到的异常如下:
System.ArgumentNullException
错误消息:值不能为空。参数名称:键
(在System.ThrowHelper.ThrowArgumentNullException(ExceptionArgument论点)
在System.Collections.Generic.Dictionary2.Insert(TKey key, TValue value, Boolean add) at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable
1源代码,Func2 keySelector, Func
2 elementSelector,IEqualityComparer1 comparer) at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable
1 source,Func2 keySelector, Func
2 elementSelector)
在Microsoft.AspNetCore.Mvc.ModelBinding.Binders.CollectionModelBinder1.<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()
--从抛出异常的前一个位置开始的堆栈跟踪结束--
在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
在Microsoft.AspNetCore.Mvc.ModelBinding.Binders.ComplexTypeModelBinder.d__10.MoveNext()
--从抛出异常的前一个位置开始的堆栈跟踪结束--
在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
在Microsoft.AspNetCore.Mvc.ModelBinding.Binders.ComplexTypeModelBinder.d__10.MoveNext()
--从抛出异常的前一个位置开始的堆栈跟踪结束--
在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
(在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task任务)
在Microsoft.AspNetCore.Mvc.ModelBinding.ParameterBinder.d__11.MoveNext()
经过数小时的调试并试图逐步了解模型绑定的内部,我的结论是ASP.NET Core不喜欢expressionEditorData.data.index
形式的“数据”字典的语法,但更喜欢这种语法:
expressionEditorData.data[0].Key = "index"
expressionEditorData.data[0].Value = 0
是否有一种使用.key = value绑定字典的方法?还有什么我可以用的模型粘合剂吗?或者为了保持这种功能--定制模型绑定是唯一的方法吗?
发布于 2022-07-12 22:41:02
您必须升级到net6。Net6有一个问题-默认情况下,您的所有属性都应该显式地标记为可空,或者另一种方法是您可以从项目文件中删除一个可空选项。
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<!--<Nullable>enable</Nullable>-->
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
或将这一行添加到配置中(启动或程序)
services.AddControllers(
options => options.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = true
https://stackoverflow.com/questions/72956921
复制相似问题