C# 返回JSon时包含内部项
我有一个Employee类,取自数据库。在这个Employee类中,我想根据Employee数据库中的managerCode添加一个Manager对象,它也是Employee。请参见下面的课程 managerCode未定义为密钥。我不需要递归,也就是说,我不需要经理的经理,等等。只有一个级别,员工和他的经理 使用.NET 4.5、c#、OData v4 我正在使用OData发回员工,但是响应中没有添加经理部分,即使它在我尝试发送的对象中 我错过了什么?在WebApiConfig里有什么 谢谢 Employee类,前4个字段直接取自数据库C# 返回JSon时包含内部项,c#,json,odata,C#,Json,Odata,我有一个Employee类,取自数据库。在这个Employee类中,我想根据Employee数据库中的managerCode添加一个Manager对象,它也是Employee。请参见下面的课程 managerCode未定义为密钥。我不需要递归,也就是说,我不需要经理的经理,等等。只有一个级别,员工和他的经理 使用.NET 4.5、c#、OData v4 我正在使用OData发回员工,但是响应中没有添加经理部分,即使它在我尝试发送的对象中 我错过了什么?在WebApiConfig里有什么 谢谢 E
Class Employee
{
[Key]
public int id { get; set; }
public string employeeCode { get; set; }
public string employeeName { get; set; }
public string managerCode { get; set; }
[NotMapped]
public Employee Manager { get; set; }
}
控制器类。GetEmployeeById(id)将与经理一起获取员工
[HttpGet]
[EnableQuery]
[ODataRoute("employeeById(id={id})")]
public IHttpActionResult employeeById([FromODataUri]int id)
{
var sets = dbContext.GetEmployeeById(id);
if (!sets.Any())
return NotFound();
return this.CreateOKHttpActionResult(sets);
}
WebApiConfig
public static void Register(HttpConfiguration config)
{
config.MapODataServiceRoute("ODataRoute", "odata",GetEdmModel(),
new DefaultODataBatchHandler(GlobalConfiguration.DefaultServer));
config.EnsureInitialized();
}
private static IEdmModel GetEdmModel()
{
var builder = new ODataConventionModelBuilder();
builder.Namespace = "employee_odata";
builder.ContainerName = "employee_odataContainer";
builder.EntitySet<Employee>("Employee");
builder.EnableLowerCamelCase();
var unboundGetEmployee = builder.Function("employeeById");
unboundGetEmployee.Returns<Employee>();
unboundGetEmployee.Parameter<int>("id");
unboundGetEmployee.Namespace = "employee_odata.Functions";
return builder.GetEdmModel();
}
控制器:
[EnableQuery]
[ODataRoute("Employee({id})")]
public IHttpActionResult employeeById([FromODataUri]int id)
{
//handle data return normally...
//I have to detect $expand=Manager, to fetch the actual Manager object.
//otherwise it's null (not linked with primary key)
}
[EnableQuery]
public IHttpActionResult Get(int id)
{
var sets = dbContext.GetEmployeeById(id);
if (!sets.Any())
return NotFound();
return this.CreateOKHttpActionResult(sets);
}
有了这些费用变化,$expand工作正常。您需要添加$expand以显示导航属性,如
localhost\odata\employee_odata.Functions.employeeById(id=1)?$expand=Manager
对于按Id获取员工,我建议您在controller中使用此方法:
[EnableQuery]
[ODataRoute("Employee({id})")]
public IHttpActionResult employeeById([FromODataUri]int id)
{
//handle data return normally...
//I have to detect $expand=Manager, to fetch the actual Manager object.
//otherwise it's null (not linked with primary key)
}
[EnableQuery]
public IHttpActionResult Get(int id)
{
var sets = dbContext.GetEmployeeById(id);
if (!sets.Any())
return NotFound();
return this.CreateOKHttpActionResult(sets);
}
然后像
localhost\odata\Employee(1)
这样的请求可以路由到该方法。您需要添加$expand以显示导航属性,如
localhost\odata\employee_odata.Functions.employeeById(id=1)?$expand=Manager
对于按Id获取员工,我建议您在controller中使用此方法:
[EnableQuery]
[ODataRoute("Employee({id})")]
public IHttpActionResult employeeById([FromODataUri]int id)
{
//handle data return normally...
//I have to detect $expand=Manager, to fetch the actual Manager object.
//otherwise it's null (not linked with primary key)
}
[EnableQuery]
public IHttpActionResult Get(int id)
{
var sets = dbContext.GetEmployeeById(id);
if (!sets.Any())
return NotFound();
return this.CreateOKHttpActionResult(sets);
}
然后像
localhost\odata\Employee(1)
这样的请求可以路由到该方法。感谢您的回答和对Get()的建议。但是,$expand=Manager不起作用,它给了我«在类型“Models.Employee”上找不到名为“Manager”的属性»。有什么想法吗?解决了。见上面的更新。这是几件小事的结合。感谢您的回答和对Get()的建议。但是,$expand=Manager不起作用,它给了我«在类型“Models.Employee”上找不到名为“Manager”的属性»。有什么想法吗?解决了。见上面的更新。这是几件小事的结合。