Skip to content

Instantly share code, notes, and snippets.

@raghuramn
Created November 28, 2012 22:49
Show Gist options
  • Save raghuramn/4165306 to your computer and use it in GitHub Desktop.
Save raghuramn/4165306 to your computer and use it in GitHub Desktop.
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; }
}
}
@raghuramn
Copy link
Author

The output on my box is,

{
  "d": {
    "__metadata": {
      "id": "http://localhost/People(guid'b269c49f-8a90-41d6-b102-7cfba3812b1c')",
      "uri": "http://localhost/People(guid'b269c49f-8a90-41d6-b102-7cfba3812b1c')",
      "type": "Application.Person"
    },
    "PersonId": "b269c49f-8a90-41d6-b102-7cfba3812b1c",
    "FirstName": "sample stri ng 2",
    "LastName": "sample string 3",
    "IsActive": true,
    "NumVacationDays": 5,
    "Salary": 6.1
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment