gpt4 book ai didi

servicestack - 为什么 servicestack 无法对 json post 请求进行模型绑定(bind)?

转载 作者:行者123 更新时间:2023-12-04 18:16:07 26 4
gpt4 key购买 nike

$.ajax({
type: 'POST',
url: "/api/student",
data:'{"x":3,"y":2}',
dataType: "json",
complete: function (r, s) {
debugger;
},

success: function(response){
debugger;
},

contentType: "application/json" // !!!!!!!!!!!! The reason of problem. I could see Json on firebug. It was false-positive for my code !

});

我已经通过 Firebug 跟踪了流。 Firebug 识别并显示 JSON 对象。

此代码访问了 RestServiceBase 的 OnPost 方法。但是模型绑定(bind)不起作用。
Json 对象和 C# 类的属性名称是否必须完全相同?

还是我错过了什么? (是的,你错过了一些东西!)

PS:我希望将 url 更改为“/api/student/json/asynconeway”,但后来出现 404 错误

最佳答案

ServiceStack 模型绑定(bind)一个 JSON POST(以及任何受支持的 Content-Types,包括 x-www-form-urlencoded)。

ServiceStack.Examples中有很多例子这样做。

This code accessed RestServiceBase's OnPost method .But model binding did not work.



您还没有显示您要绑定(bind)的 DTO。但是这个 JSON
{"x":3,"y":2}

将映射到匹配的 DTO,例如:
public class Student { 
public int X { get; set; }
public int Y { get; set; }
}

Do property names have to be exactly same on Json object and C# class?



当然,它们必须匹配名称,但是 不区分大小写 , 看上面。

PS:I've changed url to "/api/student/json/asynconeway" hopefully but then I got 404 error



这是错误的。如果您尝试使用 automatic pre-defined route ,正确的网址是:
/api/json/asynconeway/student

假设您的 请求 DTO 被称为 Student .

关于servicestack - 为什么 servicestack 无法对 json post 请求进行模型绑定(bind)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11678610/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com