为什么这个休息api返回404?

时间:2018-06-06 16:00:33

标签: c# .net git azure-devops

我遵循此示例以使用VSTS REST API:

https://docs.microsoft.com/en-us/rest/api/vsts/search/code%20search%20results/fetch%20code%20search%20results?view=vsts-rest-4.1

以下网址直接指向我org的VSTS中的MyService.MyController:

https://my-corp.visualstudio.com/DefaultCollection/USOpsInfrastructure/USOpsInfrastructure%20Team/_git/MyService?path=%2FMyService.UI%2FControllers%2FMyController.cs&version=GBmaster

我尝试使用下面的实现代码来遵循示例代码,但是返回404响应而没有详细消息。知道问题可能是什么或如何调试?

private static async Task FindMyControllerRefs()
{
    var baseUrl = "https://my-corp.almsearch.visualstudio.com";
    using (var client = GetHttpClient(baseUrl))
    {
        var request = "{ \"searchText\": \"MyController\" }";
        var projectUri = "/USOpsInfrastructure/USOpsInfrastructure%20Team/_git/MyService";
        var searchUri = "/_apis/search/codesearchresults?api-version=4.1-preview.1";
        var fullUri = projectUri + searchUri;
        var response = await client.PostAsJsonAsync(fullUri, request);

        //process the response
        if (response.IsSuccessStatusCode)
        {
            var result = (await response.Content.ReadAsAsync<string>());
        }
        else //not 200
        {
            var message = await response.Content.ReadAsStringAsync();
        }
    }
}

private static HttpClient GetHttpClient(string baseUrl)
{
    var client = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true });
    client.BaseAddress = new Uri(baseUrl);
    client.Timeout = Timeout.InfiniteTimeSpan;
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(
        new MediaTypeWithQualityHeaderValue("application/json"));
    return client;
}

1 个答案:

答案 0 :(得分:1)

代码搜索REST API应该是:

POST https://XXX.almsearch.visualstudio.com/{Project}/_apis/search/codesearchresults?api-version=4.1-preview.1

在您的情况下,请确认团队项目名称:USOpsInfrastructureMyService。如果你不确定,那么你可以试试每一个。

此外,根据我的测试,您需要在请求正文中添加"$top": 10(您可以根据需要更改数字):

var request = "{ \"searchText\": \"MyController\",\"$top\": 10 }";

以下代码适用于我:(项目名称为&#34; Git &#34;在以下示例中)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace CodeSearch
{
    class Program
    {
        public static void Main()
        {
            Task t = CodeSearch();
            Task.WaitAll(new Task[] { t });
        }
        private static async Task CodeSearch()
        {
            try
            {
                var username = "username";
                var password = "Password/PAT";

                using (var client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Accept.Add(
                        new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                        Convert.ToBase64String(
                            System.Text.ASCIIEncoding.ASCII.GetBytes(
                                string.Format("{0}:{1}", username, password))));

                    string url = "https://{instance}.almsearch.visualstudio.com/Git/_apis/search/codesearchresults?api-version=4.1-preview.1";
                    var content = new StringContent("{\"searchText\": \"OwinStartupAttribute\",  \"$top\": 10}", Encoding.UTF8, "application/json");
                    using (HttpResponseMessage response = client.PostAsync(url, content).Result)
                    {
                        response.EnsureSuccessStatusCode();
                        string responseBody = await response.Content.ReadAsStringAsync();
                        Console.WriteLine(responseBody);

                    }
                    Console.ReadLine();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            Console.ReadLine();
        }
    }
}

enter image description here