如何使用OData,WebAPI和DTO访问子对象? [英] How to access a child object using OData, WebAPI and a DTO?
问题描述
从WebAPI控制器,我从OData请求/odata/ProjectEditor?$format=json&$inlinecount=allpages&$top=10
返回ProjectEditorDTO
对象的列表:
From a WebAPI controller, I am returning a list of ProjectEditorDTO
objects from an OData request /odata/ProjectEditor?$format=json&$inlinecount=allpages&$top=10
:
[Queryable]
public virtual IHttpActionResult Get(ODataQueryOptions<Website> odataQueryOptions)
{
var userId = UserContext.Identity.UserId;
try
{
var results = odataQueryOptions.ApplyTo(_uow.Repository<Website>()
.Query()
.Get()
.Where(u => u.UserId == userId)
.OrderBy(o => o.WebsiteName)).Cast<Website>()
.Select(s => new ProjectEditorDTO()
{
// projection
WebsiteId = s.WebsiteId,
WebsiteGUID = s.WebsiteGUID,
WebsiteName = s.WebsiteName,
WebsiteNotes = s.WebsiteNotes,
DefaultContentTypeId = s.ContentType.ContentTypeId,
DefaultContentType = new ContentTypeDTO
{
ContentTypeId = s.ContentType.ContentTypeId,
Description = s.ContentType.Description
}
});
var r = results.ToList();
. . .
JSON输出:
{
"odata.metadata":"http://localhost:1981/odata/$metadata#ProjectEditor","odata.count":"2","value":[
{
"WebsiteId":6,"WebsiteName":"Jobs","WebsiteGUID":"5252fa54-5cbc-4b7e-aa74-4fc2c5c90a6a","WebsiteNotes":"notes","DefaultContentTypeId":67
},{
"WebsiteId":7,"WebsiteName":"news","WebsiteGUID":"f0717700-5900-44f7-84a4-9dde9a451285","WebsiteNotes":"notes","DefaultContentTypeId":65
}
]
}
在投影中,我定义了DefaultContentType
,但是即使在.ToList()
点可见,它也不会出现在返回给客户端的JSON数据中.
In the projection, I define DefaultContentType
but it does not appear in the JSON data returned to the client, even though it is visible at the .ToList()
point.
我不是要定义导航属性,所以我不应该(也不能)$expand
.
I'm not trying to define a navigation property, so I should not (and can't) $expand
it.
我可以在投影中简单地定义一个DefaultContentTypeId
和DefaultContentTypeDescription
,并且可以解决该问题.但是,我很想知道让DefaultContentType
对象出现在序列化的JSON对象中的问题.
I could simply define a DefaultContentTypeId
and DefaultContentTypeDescription
in the projection, and would solve the problem. However, I would like to know hot to get the DefaultContentType
object to appear in the serialized JSON object.
-更新-
ProjectEditorDTO:
ProjectEditorDTO:
public class ProjectEditorDTO
{
[ForeignKey("DefaultContentTypeId")]
public int WebsiteId { get; set; }
[Required]
[StringLength(100, MinimumLength = 3)]
[Display(Name = "Project Name")]
public string WebsiteName { get; set; }
public string WebsiteGUID { get; set; }
//public string WebsitePW { get; set; }
public string WebsiteNotes { get; set; }
public int DefaultContentTypeId { get; set; }
public string DefaultContentTypeDescription { get; set; }
public virtual ContentTypeDTO DefaultContentType { get; set; }
}
ContentTypeDTO:
ContentTypeDTO:
public class ContentTypeDTO
{
public int ContentTypeId { get; set; }
[Required]
[StringLength(100, MinimumLength=3)]
[Display(Name="Content Type")]
public string Description { get; set; }
}
推荐答案
我相信CLR类型ContentTypeDTO是元数据中的实体类型,可以通过请求
I believe the CLR type ContentTypeDTO is an entity type in your metadata, which can be got by requesting
http://localhost:1981/odata/$metadata
只需将其更改为复杂类型.更改
Just change it to a Complex Type. Change
odataConventionModelBuilder.Entity<ContentTypeDTO>()
到
odataConventionModelBuilder.ComplexType<ContentTypeDTO>();
这篇关于如何使用OData,WebAPI和DTO访问子对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!