在Web Api中如何将对象作为参数传递
// GET api/values/{can be any serilizable object}
public string Get(object data)
{
return "value";
}
[Serializable]
public class RequestData
{
public string Id { get; set; }
public string Name { get; set; }
}
答案 0 :(得分:0)
您的对象必须作为JSON在请求中发送。 [Serializable]
是不同类型的序列化。在这里,我们讨论JSON或XML序列化,它是内置的
public HttpResponseMessage Get(RequestData requestData)
{
HttpResponseMessage retMsg;
// pack your message here, select serializer {json, xml}, etc
return respMessage;
}
// [Serializable] - not needed, good old POCO is fine
public class RequestData
{
public string Id { get; set; }
public string Name { get; set; }
}