本文整理汇总了C#中JsonServiceClient.Patch方法的典型用法代码示例。如果您正苦于以下问题:C# JsonServiceClient.Patch方法的具体用法?C# JsonServiceClient.Patch怎么用?C# JsonServiceClient.Patch使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类JsonServiceClient
的用法示例。
在下文中一共展示了JsonServiceClient.Patch方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CanPerform_PartialUpdate
public void CanPerform_PartialUpdate()
{
var client = new JsonServiceClient("http://localhost:53990/api/");
// back date for readability
var created = DateTime.Now.AddHours(-2);
// Create a record so we can patch it
var league = new League() {Name = "BEFORE", Abbreviation = "BEFORE", DateUpdated = created, DateCreated = created};
var newLeague = client.Post<League>(league);
// Update Name and DateUpdated fields. Notice I don't want to update DateCreatedField.
// I also added a fake field to show it does not cause any errors
var updated = DateTime.Now;
newLeague.Name = "AFTER";
newLeague.Abbreviation = "AFTER"; // setting to after but it should not get updated
newLeague.DateUpdated = updated;
client.Patch<League>("http://localhost:53990/api/leagues/" + newLeague.Id + "?fields=Name,DateUpdated,thisFieldDoesNotExist", newLeague);
var updatedLeague = client.Get<League>(newLeague);
Assert.AreEqual(updatedLeague.Name, "AFTER");
Assert.AreEqual(updatedLeague.Abbreviation, "BEFORE");
Assert.AreEqual(updatedLeague.DateUpdated.ToString(), updated.ToString(), "update fields don't match");
Assert.AreEqual(updatedLeague.DateCreated.ToString(), created.ToString(), "created fields don't match");
// double check
Assert.AreNotEqual(updatedLeague.DateCreated, updatedLeague.DateUpdated);
}
示例2: Test_PATCH_PASS
public void Test_PATCH_PASS()
{
var restClient = new JsonServiceClient(serviceUrl);
// register callback to grab the Location: header when executed
string lastResponseLocation = "";
HttpStatusCode lastResponseStatusCode = 0;
restClient.LocalHttpWebResponseFilter = httpRes =>
{
lastResponseLocation = httpRes.Headers[HttpHeaders.Location];
lastResponseStatusCode = httpRes.StatusCode;
};
// dummy data
var newemp1 = new Employee()
{
Id = 123,
Name = "Kimo",
StartDate = new DateTime(2015, 7, 2),
CubicleNo = 4234,
Email = "[email protected]",
};
restClient.Post<object>("/employees", newemp1);
var emps = restClient.Get<List<Employee>>("/employees");
var emp = emps.First();
var empPatch = new Operations.EmployeePatch();
empPatch.Add(new Operations.JsonPatchElement()
{
op = "replace",
path = "/title",
value = "Kahuna Laau Lapaau",
});
empPatch.Add(new Operations.JsonPatchElement()
{
op = "replace",
path = "/cubicleno",
value = "32",
});
restClient.Patch<object>(string.Format("/employees/{0}", emp.Id), empPatch);
var empAfterPatch = restClient.Get<Employee>(string.Format("/employees/{0}", emp.Id));
Assert.NotNull(empAfterPatch);
// patched
Assert.Equal("Kahuna Laau Lapaau", empAfterPatch.Title);
Assert.Equal("32", empAfterPatch.CubicleNo.ToString());
// unpatched
Assert.Equal("[email protected]", empAfterPatch.Email);
}
示例3: Test_PATCH_unsupported_cast_PASS
public void Test_PATCH_unsupported_cast_PASS()
{
var restClient = new JsonServiceClient(serviceUrl);
// dummy data
var newemp1 = new Employee()
{
Id = 123,
Name = "Kimo",
StartDate = new DateTime(2015, 7, 2),
CubicleNo = 4234,
Email = "[email protected]",
};
restClient.Post<object>("/employees", newemp1);
var emps = restClient.Get<List<Employee>>("/employees");
var emp = emps.First();
var empPatch = new Operations.EmployeePatch();
// float not currently supported by this example code
empPatch.Add(new Operations.JsonPatchElement()
{
op = "replace",
path = "/longitude",
value = "2.123",
});
restClient.Patch<object>(string.Format("/employees/{0}", emp.Id), empPatch);
}
示例4: Test_PATCH_unsupported_cast_FAIL
public void Test_PATCH_unsupported_cast_FAIL()
{
var restClient = new JsonServiceClient(serviceUrl);
// dummy data
var newemp1 = new Employee()
{
Id = 123,
Name = "Kimo",
StartDate = new DateTime(2015, 7, 2),
CubicleNo = 4234,
Email = "[email protected]",
};
restClient.Post<object>("/employees", newemp1);
var emps = restClient.Get<List<Employee>>("/employees");
var emp = emps.First();
var empPatch = new Operations.EmployeePatch();
// double not currently supported by this example code
empPatch.Add(new Operations.JsonPatchElement()
{
op = "replace",
path = "/othernumber",
value = "3.1415927",
});
Assert.Throws<WebServiceException>(delegate
{
// InvalidCastException
restClient.Patch<object>(string.Format("/employees/{0}", emp.Id), empPatch);
});
}