Created
November 28, 2012 22:49
-
-
Save raghuramn/4165306 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Net.Http; | |
using System.Net.Http.Headers; | |
using System.Web.Http; | |
using System.Web.Http.OData; | |
using System.Web.Http.OData.Builder; | |
using System.Web.Http.OData.Formatter; | |
namespace Application | |
{ | |
public class Program | |
{ | |
public static void Main() | |
{ | |
HttpConfiguration config = new HttpConfiguration(); | |
ODataConventionModelBuilder builder = new ODataConventionModelBuilder(); | |
builder.EntitySet<Person>("People"); | |
ODataMediaTypeFormatter formatter = new ODataMediaTypeFormatter(builder.GetEdmModel()); | |
formatter.PatchKeyMode = PatchKeyMode.Patch; | |
config.Formatters.Insert(0, formatter); | |
config.Routes.MapHttpRoute("default", "{controller}"); | |
config.Routes.MapHttpRoute(ODataRouteNames.GetById, "{controller}({id})"); | |
config.Routes.MapHttpRoute(ODataRouteNames.Default, "{controller}"); | |
HttpServer server = new HttpServer(config); | |
HttpClient client = new HttpClient(server); | |
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Put, "http://localhost/People"); | |
request.Content = new StringContent("{ 'PersonId': 'b269c49f-8a90-41d6-b102-7cfba3812b1c', 'FirstName': 'sample string 2', 'LastName': 'sample string 3', 'IsActive': true, 'NumVacationDays': 5, 'Salary': 6.1 }"); | |
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json;odata=verbose"); | |
Console.WriteLine(client.SendAsync(request).Result.Content.ReadAsStringAsync().Result); | |
} | |
} | |
public class PeopleController : ApiController | |
{ | |
public Person Put(Delta<Person> person) | |
{ | |
var p = person.GetEntity(); | |
Person existingPerson = new Person { PersonId = p.PersonId }; | |
person.Patch(existingPerson); | |
return existingPerson; | |
} | |
} | |
public class Person | |
{ | |
public Guid PersonId { get; set; } | |
public string FirstName { get; set; } | |
public string LastName { get; set; } | |
public bool IsActive { get; set; } | |
public int NumVacationDays { get; set; } | |
public double Salary { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The output on my box is,