2

上下文:在 Asp.net Core 2.1 下的 WebAPI 中,我必须创建一个 POST 端点 ,[server]/MyController/{Parameter1}/MoreRouteThing/. 我必须创建一个自定义IInputFormatter,因为默认格式化程序无法读取正文。

问题:为了能够格式化输入,IInputFormatter需要知道Parameter1.

我实现了一个IModelBinder处理这个模型的自定义,startup.cs使用自定义连接所有东西IModelBinderProvider(可能有点矫枉过正,但我​​想了解整个链条。)

在自定义IModelBinder中,我可以{Parameter1}使用类似的东西进行访问bindingContext.ActionContext.RouteData.Values["Parameter1"],但我不知道如何将其传递给IInputFormatter. 前者将 an 传递InputFormatterContext给后者,但该上下文对象中的任何内容似乎都不适合存储额外信息。

所以问题:如何将数据从 传递IModelBinderIInputFormatter?我是否应该直接从 解析 url/路由IInputFormatter,从而让它知道它在整个过程中的“位置”?(对我来说似乎不干净。)

4

1 回答 1

0

所有格式化程序的列表通过模型绑定器的构造函数传输,并在将来选择与指定条件匹配的格式化程序。更多细节可以在源代码中找到: 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

于 2019-07-07T14:50:05.827 回答