如何从 Azure DevOps Repo 打开 txt 文件并将其存储在字符串中?

How to open txt file from Azure DevOps Repo and store it in string?

我在特定存储库中有多个 txt 文件。想要获取所有这些文件的 body/content 并将其存储在 C# 代码的列表中。如何通过 C# 代码从 Azure Dev Ops 获取 content/body 文件?

当我尝试通过代码执行此操作时:

var buildUrl = $"https://dev.azure.com/CodeRepo/Project/_apis/git/repositories/Indexes/items?path=/server/database/table/index.sql&download=true&versionType=Branch&versionOptions=None&api-version=5.0";

                        using (HttpResponseMessage responseFrom = client.GetAsync(
                              buildUrl).Result)
                        {
                            responseFrom.EnsureSuccessStatusCode();
                            string responseBody = await responseFrom.Content.ReadAsStringAsync();
                            Console.WriteLine(responseBody);
                            Console.ReadLine();
                        }

我正在进入结果 json 文件:

{
    "objectId": "ac759bcb819e39855a7e82a8635d066c50b55555",
    "gitObjectType": "blob",
    "commitId": "da1d095a7cc8193563c9390dc9027aa4b6366333",
    "path": "/server/database/table/index.sql",
    "url": "https://dev.azure.com/CodeRepo/123/_apis/git/repositories/456/items?path=%2server%2Fdatabase%2Ftable%2Findex.sql&versionType=Branch&versionOptions=None",
    "_links": {
        "self": {
            "href": "https://dev.azure.com/CodeRepo/123/_apis/git/repositories/456/items?path=%2Fserver%2Fdatabase%2Ftable%2index.sql&versionType=Branch&versionOptions=None"
        },
        "repository": {
            "href": "https://dev.azure.com/CodeRepo/123/_apis/git/repositories/456"
        },
        "blob": {
            "href": "https://dev.azure.com/CodeRepo/123/_apis/git/repositories/456/blobs/ac759bcb819e39855a7e82a8635d066c50b55555"
        }
    }
}

您可以使用Rest Api to get file content. Here is the answer to your question:

在您的示例中,未表示客户端创建。我想,你使用类似的东西:

client.DefaultRequestHeaders.Accept.Add(
                        new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

尝试删除此 header。