我有一个ASP .NET MVC应用程序,允许用户通过调用API来创建工作项。创建工作项后,我将重定向到显示当前工作项的页面(另一个API调用)。但是,检索工作项的API请求处理速度比创建工作项更快,导致显示不显示刚刚创建的工作项。我目前正在使用thread.sleep进行测试,但希望以正确的方式处理这个问题。
这是我在创建工作项后处理提交按钮的控制器操作
WICreator addNewWI = new WICreator();
addNewWI.CreateWorkItem(model.SystemTitle, model.SystemDescription);
Thread.Sleep(2000);
return RedirectToAction("WorkItems", "Home");
这是调用API来创建工作项
public void CreateWorkItem(string title, string description)
{
string personalAccessToken = "xxx"
string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{0}:{1}", "xxx", personalAccessToken)));
string project = "xx"
string path = "xx"
Object[] patchDocument = new Object[3];
patchDocument[0] = new { op = "add", path = "/fields/System.Title", value = title };
patchDocument[1] = new { op = "add", path = "/fields/System.AreaPath", value = path };
patchDocument[2] = new { op = "add", path = "/fields/System.Description", value = description };
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", credentials);
var patchValue = new StringContent(JsonConvert.SerializeObject(patchDocument), Encoding.UTF8, "application/json-patch+json");
var method = new HttpMethod("PATCH");
var request = new HttpRequestMessage(method, "https://xx.xx.com/xx/_apis/wit/workitems/xxxapi-version=1.0") { Content = patchValue };
var response = client.SendAsync(request).Result;
if (response.IsSuccessStatusCode)
{
var result = response.Content.ReadAsStringAsync().Result;
}
}
}
答案 0 :(得分:0)
创建工作项的响应包含新工作项的必要信息,因此处理此问题的简单工作流程为: