[英]Passing an instance of user-defined class as data to a ServiceStack PUT
I'm relatively new to ServiceStack and RESTful services in general and I'm running into the following roadblock: 一般而言,我对ServiceStack和RESTful服务还比较陌生,并且遇到了以下障碍:
I have a ServiceStack request object similar to the following (in C#): 我有一个类似于以下内容的ServiceStack请求对象(在C#中):
[Authenticate]
[Route("/product", "POST")]
[Route("/product/{productId}", "GET,PUT,DELETE")]
public class SomeProductRequest: IReturn<Dto.Product>
{
public int ProductId{ get; set; }
public ProductStuff SomeProductProperties { get; set; }
}
I can build the object in Javascript on the client-side preparing for a PUT like so: 我可以在客户端用Javascript构建对象,以准备进行PUT,如下所示:
var product = {
productId: 100,
someProductProperties: { id:5, name:"My Name" }
};
However, whenever the request reaches the server, the SomeProductProperties
will always be null
. 但是,无论何时请求到达服务器,
SomeProductProperties
都将始终为null
。 In fact, I'm unable to pass user-defined types to the server at all. 实际上,我根本无法将用户定义的类型传递给服务器。 Only basic, baked-in .NET types seem to go over the wire (int, string, etc.)
似乎只有基本的内置.NET类型会遍历整个网络(整数,字符串等)。
Is there some trick to having a property of a user-defined class in the request object? 在请求对象中具有用户定义类的属性是否有一些技巧?
All help is very much appreciated! 非常感谢所有帮助! Thanks!
谢谢!
Ended up figuring this out after discussing with a colleague. 与同事讨论后最终解决了这个问题。 For the benefit of everybody else, here is what I did:
为了所有人的利益,这是我所做的:
When sending a complex type across the wire, you need to wrap your js object that contains the complex type using JSON.stringify()
and pass the resulting string instead: 通过网络发送复杂类型时,您需要使用
JSON.stringify()
包装包含复杂类型的js对象,然后传递结果字符串:
var updatedUser = self.buildUserObjectFromGui();
$.ajax({
type: 'PUT',
contentType: "application/json",
data: JSON.stringify(updatedUser),
url: 'internal/user/' + this.options.userId
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.