2
我的實體QueryFilter
有一個屬性,它包含序列化的json。 在QueryFilterDetailDTOController
我想反序列化JSON,用一些數據來豐富它,並用新的JSON數據返回一個DTO。Odata Asp.NET WebApi返回實體中的json屬性/ DTO
我希望能夠直接使用豐富的JSON對象作爲javascript中返回對象的屬性(而不是字符串)。我怎樣才能做到這一點?
我試圖將表達式的類型設置爲JObject
,但模型構建器引發異常(因爲遞歸類型)。這是正確的方法,還是有更好的方法?
DTO
public class QueryFilterDetailDTO
{
public int ID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Context { get; set; }
public JObject Expression { get; set; }
public QueryFilterDetailDTO(QueryFilter queryFilter)
{
ID = queryFilter.ID;
Name = queryFilter.Name;
Description = queryFilter.Description;
Context = queryFilter.Context;
}
}
控制器
public class QueryFilterDetailDTOController : ODataController
{
private readonly IQueryFilterService _service;
public QueryFilterDetailDTOController(IQueryFilterService service)
{
_service = service;
}
[HttpGet]
[EnableQuery(MaxNodeCount = 1000)]
public HttpResponseMessage Get([FromODataUri] int key)
{
var queryFilter = _service.Find(key);
var dto = new QueryFilterDetailDTO(queryFilter);
dto.Expression = JObject.Parse(queryFilter.SerializedFilter);
dto.Expression["Test"] = 1;
return Request.CreateResponse(HttpStatusCode.OK, dto);
}
}
創建模型
private static IEdmModel GetModel()
{
ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<QueryFilterDetailDTO>("QueryFilterDetailDTO").EntityType.HasKey(dto => dto.ID);
builder.GetEdmModel();
}