ASP.NET Core MVC Model Binding: Custom Binders

详见: https://www.dotnetcurry.com/aspnet-mvc/1368/aspnet-core-mvc-custom-model-binding

 

1. ASP.Net MVC、ASP.Net WebAPI以及ASP.Net Core MVC中的模型绑定过程并不相同。(见下图)

2. 默认情况下,并不是每个绑定源都被选中。有些模型绑定需要您特别启用绑定源。例如,当从Body绑定时,您需要在模型/动作参数中添加[FromBody](比如前端通过ajax以“application/json”的contentType调用时就是从Body绑定的情况),否则将不会使用BodyModelBinder;尽管Form数据是在Body中以URL-encode字符发布,但它是一种特殊情况,框架仍然将其解析为值提供程序不需要添加[FromBody]。  另外需要添加说明的还有模型参数是Headers、Files的情况。从Body绑定时,将从传递到给后端的Body数据反序列化成模型对象作为模型参数。

ASP.NET Core MVC Model Binding: Custom Binders

 

ASP.Net MVC模型绑定过程:

ASP.NET Core MVC Model Binding: Custom Binders

 

ASP.Net WebAPI 模型绑定过程:

ASP.NET Core MVC Model Binding: Custom Binders

 

 ASP.Net Core MVC中的模型绑定过程:

ASP.NET Core MVC Model Binding: Custom Binders