제목 : ASP.NET Web API 계층형 구조 JSON 생성 코드 샘플
글번호:
|
|
336
|
작성자:
|
|
레드플러스
|
작성일:
|
|
2014/03/17 오후 6:47:00
|
조회수:
|
|
3470
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
namespace WebApiJsonMulti.Controllers
{
public class Player
{
public virtual int Id { get; set; }
public virtual int TeamId { get; set; }
public virtual string Name { get; set; }
}
public class Team
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual double Rate { get; set; }
public virtual int Version { get; set; }
public virtual ICollection<Player> Players { get; set; }
}
public class ValuesController : ApiController
{
// GET api/values
public IEnumerable<Team> Get()
{
return Data();
}
public List<Team> Data()
{
List<Team> Teams = new List<Team>();
Teams.AddRange(Enumerable.Range(1, 3).Select(i =>
new Team
{
Id = i,
Name = "Team " + i,
Rate = i * Math.PI / 10,
Players = Enumerable.Range(1, 5).Select(j =>
new Player
{
Id = 11 * (i - 1) + j,
TeamId = i,
Name = string.Format("Team {0} Player {1}", i, j)
}).ToList()
}
));
return Teams;
}
// GET api/values/5
public string Get(int id)
{
return "value";
}
// POST api/values
public void Post([FromBody]string value)
{
}
// PUT api/values/5
public void Put(int id, [FromBody]string value)
{
}
// DELETE api/values/5
public void Delete(int id)
{
}
}
}