我不确定它是否会有所帮助,但我在上面的评论中提到我最终使用自定义模型绑定器来执行此操作。我挖出了我认为是引发这个问题的原始代码,这就是我最终得到的:
public async Task<ActionResult> Post(dynamic request)
{
return await ExecuteRequest(request, "application/json");
}
和一个自定义模型绑定器如下(它适用于称为“发布”或“公共”的操作,尽管您可以选择自己的约定 - 并在所有其他操作上恢复为默认值)
public class MyModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var actionName = controllerContext.RouteData.Values["Action"];
if (controllerContext.Controller.GetType() == typeof(MyController) && actionName != null &&
(string.Compare(actionName.ToString(), "post", StringComparison.OrdinalIgnoreCase) == 0 ||
string.Compare(actionName.ToString(), "public", StringComparison.OrdinalIgnoreCase) == 0))
{
string contentText;
using (var stream = controllerContext.HttpContext.Request.InputStream)
{
stream.Seek(0, SeekOrigin.Begin);
using (var reader = new StreamReader(stream))
contentText = reader.ReadToEnd();
}
if (string.IsNullOrEmpty(contentText)) return (null);
return JObject.Parse(contentText);
}
return base.BindModel(controllerContext, bindingContext);
}
}
然后在 Application_Start 的开头注册自定义模型绑定器:
System.Web.Mvc.ModelBinders.Binders.DefaultBinder = new MyModelBinder();