5)ASP.NET Web API Scaffolding(模板)

时间:2022-01-11 03:39:37

public class TeamsController : ApiController{

    private ApressRecipesWebApiContext db = new ApressRecipesWebApiContext();

    // GET: api/Teams    public IQueryable<Team> GetTeams()

    {

        return db.Teams;

    }

    // GET: api/Teams/5    [ResponseType(typeof(Team))]

    public async Task<IHttpActionResult> GetTeam(int id)

    {

        Team team = await db.Teams.FindAsync(id);

        if (team == null)

        {

            return NotFound();

        }

        return Ok(team);

    }

    // PUT: api/Teams/5    [ResponseType(typeof(void))]

    public async Task<IHttpActionResult> PutTeam(int id, Team team)

    {

        if (!ModelState.IsValid)

        {

            return BadRequest(ModelState);

        }

        if (id != team.Id)

        {

            return BadRequest();

        }

        db.Entry(team).State = EntityState.Modified;

        try        {

            await db.SaveChangesAsync();

        }

        catch (DbUpdateConcurrencyException)

        {

            if (!TeamExists(id))

            {

                return NotFound();

            }

            else            {

                throw;

            }

        }

        return StatusCode(HttpStatusCode.NoContent);

    }

    // POST: api/Teams    [ResponseType(typeof(Team))]

    public async Task<IHttpActionResult> PostTeam(Team team)

    {

        if (!ModelState.IsValid)

        {

            return BadRequest(ModelState);

        }

        db.Teams.Add(team);

        await db.SaveChangesAsync();

        return CreatedAtRoute("DefaultApi", new { id = team.Id }, team);

    }

    // DELETE: api/Teams/5    [ResponseType(typeof(Team))]

    public async Task<IHttpActionResult> DeleteTeam(int id)

    {

        Team team = await db.Teams.FindAsync(id);

        if (team == null)

        {

            return NotFound();

        }

        db.Teams.Remove(team);

        await db.SaveChangesAsync();

        return Ok(team);

    }

    protected override void Dispose(bool disposing)

    {

        if (disposing)

        {

            db.Dispose();

        }

        base.Dispose(disposing);

    }

    private bool TeamExists(int id)

    {

        return db.Teams.Count(e => e.Id == id) > 0;

    }