假设我们有一个桌面应用程序的运行时数据类,如下所示:
public class A
{
public string A_string;
public int A_int;
public B BInstance;
public C CInstance;
}
我们在c#中向此应用程序添加了REST API。 像这样:
[Route("service-path-for-get-a/{aId}")]
[HttpGet]
public HttpResponseMessage Geta(string aId)
{
var dataProvider = new DataProviderFactory().GetSqliteDataProvider("adb.sqlite");
A queryResult = dataProvider.GetAnA(aId);
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, queryResult);
return response;
}
HttpResponseMessage返回json,例如:
{
"A_string": "xyz",
"A_int": 0.4,
"BInstance": {
"x": 80,
"x": 2000,
"...": 4
},
"CInstance": {
"x": 80,
"x": 2000,
"...": 4
},
}
我希望在不更改类A的情况下,用指向REST API的URI链接替换整个BInstance和CInstance数据(类似于HATEOAS)。 可能吗?如果怎么办? 因为我不想将属性添加到A类中,而这仅是REST API json序列化所需的属性,而在运行时则不需要。
我想要的东西是这样的:
{
"A_string": "xyz",
"A_int": 0.4,
"BInstance": {
"Links": [
{
"Href": "http://localhost:57009/api/deliveries/dev1",
"Rel": "self",
"Method": "GET"
},
{
"Href": "http://localhost:57009/api/deliveries/dev1/status?status=delivered",
"Rel": "status-delivered",
"Method": "PUT"
}
]
},
"CInstance": {
"Links": [
{
"Href": "http://localhost:57009/api/deliveries/dev1",
"Rel": "self",
"Method": "GET"
},
{
"Href": "http://localhost:57009/api/deliveries/dev1/status?status=delivered",
"Rel": "status-delivered",
"Method": "PUT"
}
]
},
}
我必须编写自己的序列化程序吗?或什么是解决此问题的正确方法。
谢谢