C# 使用TFS API更新工作项时出错(HTTP状态400)

C# 使用TFS API更新工作项时出错(HTTP状态400),c#,visual-studio,api,tfs,tfs-sdk,C#,Visual Studio,Api,Tfs,Tfs Sdk,我正在尝试使用TFS API根据更新TFS上工作项的字段。当使用页面上列出的示例代码时,api“Update work items”出现问题,响应的返回状态代码为400。不管我做了什么,状态码总是400,有人能给我一些帮助吗 using System; using System.Net.Http; using System.Net.Http.Headers; using System.Text; using Newtonsoft.Json; public void UpdateWorkIt

我正在尝试使用TFS API根据更新TFS上工作项的字段。当使用页面上列出的示例代码时,api“Update work items”出现问题,响应的返回状态代码为400。不管我做了什么,状态码总是400,有人能给我一些帮助吗

using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using Newtonsoft.Json;



public void UpdateWorkItemUpdateField()
{
   string _personalAccessToken = "xxxxxxxxxxxxxx";
   string _credentials = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "", _personalAccessToken)));
   string _id = "111";

   Object[] patchDocument = new Object[3];

   patchDocument[0] = new { op = "test", path = "/rev", value = "1" };
   patchDocument[1] = new { op = "add", path = "/fields/Microsoft.VSTS.Common.Priority", value = "2" };
   patchDocument[2] = new { op = "add", path = "/fields/System.History", value = "Changing priority" };

  using (var client = new HttpClient())
   {
       client.DefaultRequestHeaders.Accept.Clear();
   client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
   client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", _credentials);

   var patchValue = new StringContent(JsonConvert.SerializeObject(patchDocument), Encoding.UTF8, "application/json"); // mediaType needs to be application/json-patch+json for a patch call

  var method = new HttpMethod("PATCH");
   var request = new HttpRequestMessage(method, "https://accountname.visualstudio.com/_apis/wit/workitems/" + _id + "?api-version=2.2") { Content = patchValue };
   var response = client.SendAsync(request).Result;

   if (response.IsSuccessStatusCode)
   {
       var result = response.Content.ReadAsStringAsync().Result;
   }
   }
}

您使用了错误的媒体类型,它应该是“application/json patch+json”而不是“application/json”

var patchValue = new StringContent(JsonConvert.SerializeObject(patchDocument), Encoding.UTF8, "application/json-patch+json");
在代码示例中,它已经注意到您:

//对于补丁调用,mediaType需要是application/json patch+json


您正在调用修补程序调用,因此请将mediaType更改为
application/json Patch+json

谢谢您的帮助,在我更改mediaType后,错误代码400消失。之后,我试图更改某个工作项的/fields/System.Description,但http状态的返回值是400,您知道错误代码吗?谢谢@RickyXRQ在你发布新的一期后,在这里发布新的链接,我会帮助你@Tingting0929 MSFTI需要测试您的代码,我会尽快将答案打包发布。