无法将 ModelBinderAttribute 和 FromBody 结合在控制器操作方法中。

huangapple go评论67阅读模式
英文:

fail to combine ModelBinderAttribute and FromBody for controller action method

问题

Following a .Net Framework to .Net Core MVC migration, 在进行 .Net Framework 到 .Net Core MVC 迁移后,

The Combination between [Modelbinder] with a second complex type in a controller action parameter does not seem to work anymore. 使用 [Modelbinder] 与控制器操作参数中的第二个复杂类型的组合似乎不再起作用。

E.g. of method which I try to call: 例如,我尝试调用的方法:

public ActionResult GetResult([ModelBinder(typeof(ComplexDynamicModelBinder))] dynamic dynamicFormModel, [FromBody] RandomComplexType complexType) { 
// Do Something 
}

E.g. of call from Js: 例如,来自 JavaScript 的调用:

   ['__RequestVerificationToken', 'myveriftoken'],
   ['x-requested-with', 'XMLHttpRequest'],
   ['Content-Type', 'application/json']
]);
fetch(urlToAction, {
   method: 'POST',
   body: JSON.stringify({
      dynamicFormModel: { someProperties: "values" },
      complexType: { someProperties: "values" }
   }),
   headers: fetchHeaders
})
.then(console.log);

All my different attempts always get a 415 ... 所有不同尝试始终收到 415 错误...

Thank you for your future help. 感谢您未来的帮助。

英文:

Following a .Net Framework to .Net Core MVC migration,
The Combination between [Modelbinder] with a second complex type in a controller action parameter does not seem to work anymore.

E.g. of method which I try to call:

[HttpPost]
public ActionResult GetResult([ModelBinder(typeof(ComplexDynamicModelBinder))] dynamic dynamicFormModel, [FromBody] RandomComplexType complexType) { 
// Do Something 
}

E.g. of call from Js:

const fetchHeaders = new Headers([
   ['__RequestVerificationToken', 'myveriftoken'],
   ['x-requested-with', 'XMLHttpRequest'],
   ['Content-Type', 'application/json']
]);
fetch(urlToAction, {
   method: 'POST',
   body: JSON.stringify({
      dynamicFormModel: { someProperties: "values" },
      complexType: { someProperties: "values" }
   }),
   headers: fetchHeaders
})
.then(console.log);

All my different attempts always get a 415 ...

Thank you for your future help

答案1

得分: 0

415通常是数据类型不匹配的错误。由于我不知道你的模型和ComplexDynamicModelBinder是什么,我没有使用[ModelBinder],而是基于你的数据构建了一个模型,你可以将其用作参考。

模型:

public class RandomComplexType
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class ViewModel
{
    public RandomComplexType complexType { get; set; }
}

JavaScript:

const fetchHeaders = new Headers([
    ['__RequestVerificationToken', 'myveriftoken'],
    ['x-requested-with', 'XMLHttpRequest'],
    ['Content-Type', 'application/json']
]);
fetch('/Home/GetResult', {
    method: 'POST',
    body: JSON.stringify({
        dynamicFormModel: { someProperties: "values" },
        complexType: { "Id": 1, "Name": "Name1" }
    }),
    headers: fetchHeaders
})
.then(console.log);

测试结果:

你可以在浏览器的开发工具中检查你发送的数据是什么样的,以及是否符合标准:

无法将 ModelBinderAttribute 和 FromBody 结合在控制器操作方法中。

无法将 ModelBinderAttribute 和 FromBody 结合在控制器操作方法中。

英文:

415 is usually a data type mismatch error. Since I don't know what your model and ComplexDynamicModelBinder are, I didn't use [ModelBinder] and built a model based on your data, you can use it as a reference.

Model:

public class RandomComplexType
{
    public int Id { get; set; }
    public string Name { get; set; }
        
}
public class ViewModel
{ 
    public RandomComplexType complexType { get; set; }
}

JavaScript:

const fetchHeaders = new Headers([
   ['__RequestVerificationToken', 'myveriftoken'],
   ['x-requested-with', 'XMLHttpRequest'],
   ['Content-Type', 'application/json']
]);
fetch('/Home/GetResult', {
   method: 'POST',
   body: JSON.stringify({
        dynamicFormModel: { someProperties: "values" },
        complexType: { "Id": 1, "Name": "Name1" }  
   }),
   headers: fetchHeaders
})
.then(console.log);

Test Result:
无法将 ModelBinderAttribute 和 FromBody 结合在控制器操作方法中。

You can check what the data you send looks like and whether it complies with the standard in the developer tools of the browser:
无法将 ModelBinderAttribute 和 FromBody 结合在控制器操作方法中。

huangapple
  • 本文由 发表于 2023年4月13日 22:36:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76006714.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定