Skip to content

Instantly share code, notes, and snippets.

@motowilliams
Created February 22, 2012 21:37
Show Gist options
  • Save motowilliams/1887545 to your computer and use it in GitHub Desktop.
Save motowilliams/1887545 to your computer and use it in GitHub Desktop.
using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using ContactManager.Models;
namespace ContactManager.Controllers
{
public class ContactsController : ApiController
{
private readonly IContactRepository repository;
public ContactsController(IContactRepository repository)
{
this.repository = repository;
}
public IQueryable<Contact> Get()
{
return this.repository.GetAll().AsQueryable();
}
public HttpResponseMessage<Contact> Post(Contact contact)
{
var post = this.repository.Post(contact);
var response = new HttpResponseMessage<Contact>(contact) { StatusCode = HttpStatusCode.Created };
string uri = Url.Route(null, new { id = post.ContactId, controller = "Contact" });
response.Headers.Location = new Uri(Request.RequestUri, uri);
return response;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment