英文:
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);
测试结果:
你可以在浏览器的开发工具中检查你发送的数据是什么样的,以及是否符合标准:
英文:
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);
You can check what the data you send looks like and whether it complies with the standard in the developer tools of the browser:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论