Context:在Asp.net Core2.1下的WebAPI中,我必须创建一个POST端点[server]/MyController/{Parameter1}/MoreRouteThing/。我必须创建一个自定义IInputFormatter,因为默认格式化程序无法读取主体。
Problem:为了能够格式化输入,IInputFormatter需要知道Parameter1的值。
我实现了一个自定义IModelBinder,它处理这个模型,使用自定义IModelBinderProvider连接startup.cs中的所有东西(可能过高,但我想了解整个链)。
在自定义IModelBinder中,我可以使用类似的bindingContext.ActionContext.RouteData.Values["Parameter1"]访问{Parameter1},但是我不知道如何将它传递给IInputFormatter。前者将一个InputFormatterContext传递给后者,但是上下文对象中似乎没有任何东西适合存储额外的信息。
那么问题:如何将数据从IModelBinder传递到IInputFormatter?我应该直接从IInputFormatter解析url/路由,从而让它知道它在整个流程中的位置吗?(在我看来是不干净的。)
发布于 2019-07-07 14:50:05
所有格式化程序的列表都通过模型绑定器的构造函数传输,并在将来选择与指定条件匹配的构造器。更多细节可以在源代码中找到:https://github.com/aspnet/AspNetCore/blob/master/src/Mvc/Mvc.Core/src/ModelBinding/Binders/BodyModelBinder.cs和https://github.com/aspnet/AspNetCore/blob/master/src/Mvc/Mvc.Core/src/ModelBinding/Binders/BodyModelBinderProvider.cs
https://stackoverflow.com/questions/51830213
复制相似问题