返回资源组对象并列表< string>使用Azure功能C#

发布于 2025-02-13 09:02:54 字数 2039 浏览 1 评论 0原文

我正在创建一个API,可以在给定订阅中检索资源组列表或检索资源组ID,但是

[Function("getRg")]
public async Task<HttpResponseData> GetRgs([HttpTrigger(AuthorizationLevel.Function, "get", Route = "rg")] HttpRequestData req,
        string? rg, string sub,
        FunctionContext functionContext)
{
    ArmClient client = new ArmClient(new DefaultAzureCredential());

    SubscriptionCollection subscriptions = client.GetSubscriptions();
    SubscriptionResource subscription = await subscriptions.GetAsync(sub);

    ResourceGroupCollection resourceGroups = subscription.GetResourceGroups();

    if (!string.IsNullOrEmpty(rg))
    {
        string resJson = "";
        ResourceGroupResource resourceGroup = await resourceGroups.GetAsync(rg);
        resourceGroup = await resourceGroup.GetAsync();
        resJson = JsonSerializer.Serialize(resourceGroup.Data.Id);

        _logger.LogInformation($"requested RG: ${resJson}");

        var response = req.CreateResponse(HttpStatusCode.OK);
        response.Headers.Add("Content-Type", "text/plain; charset=utf-8");
        response.WriteString(resJson);

        return response;
    }
    else 
    {
        var rgList = new List<string>();

        foreach (var rgItem in resourceGroups)
        {
            rgList.Add(rgItem.Data.Name);
        }

        _logger.LogInformation($"rg list: {rgList}");

        var response = req.CreateResponse(HttpStatusCode.OK);
        response.Headers.Add("Content-Type", "text/html, charset=utf-8");
        await response.WriteAsJsonAsync(rgList);

        return response;
    }
}

当我尝试通过不提供RG名称参数来获取列表时, 我无法返回这两个以下错误:

system.private.corelib:执行函数时异常:functions.getrg。 system.private.corelib:结果:失败

异常:system.AggregateException:发生一个或多个错误。 (值'text/plain,charset = utf-8'的格式无效。)

在这种情况下,响应标头中的格式应该是什么?我在做对吗?

当我尝试通过使用此行来获得一个RG的ID时,我不理解的第二种行为也是如此:

resJson = JsonSerializer.Serialize(resourceGroup.Data.Id);

我希望只能获得ID,但是我会得到一个不同的大对象。

感谢您的帮助!

I am creating an API where I can retrieve the list of resource groups in a given subscription or retrieve a resource group ID, but I am unable to return both

[Function("getRg")]
public async Task<HttpResponseData> GetRgs([HttpTrigger(AuthorizationLevel.Function, "get", Route = "rg")] HttpRequestData req,
        string? rg, string sub,
        FunctionContext functionContext)
{
    ArmClient client = new ArmClient(new DefaultAzureCredential());

    SubscriptionCollection subscriptions = client.GetSubscriptions();
    SubscriptionResource subscription = await subscriptions.GetAsync(sub);

    ResourceGroupCollection resourceGroups = subscription.GetResourceGroups();

    if (!string.IsNullOrEmpty(rg))
    {
        string resJson = "";
        ResourceGroupResource resourceGroup = await resourceGroups.GetAsync(rg);
        resourceGroup = await resourceGroup.GetAsync();
        resJson = JsonSerializer.Serialize(resourceGroup.Data.Id);

        _logger.LogInformation(
quot;requested RG: ${resJson}");

        var response = req.CreateResponse(HttpStatusCode.OK);
        response.Headers.Add("Content-Type", "text/plain; charset=utf-8");
        response.WriteString(resJson);

        return response;
    }
    else 
    {
        var rgList = new List<string>();

        foreach (var rgItem in resourceGroups)
        {
            rgList.Add(rgItem.Data.Name);
        }

        _logger.LogInformation(
quot;rg list: {rgList}");

        var response = req.CreateResponse(HttpStatusCode.OK);
        response.Headers.Add("Content-Type", "text/html, charset=utf-8");
        await response.WriteAsJsonAsync(rgList);

        return response;
    }
}

When I try to get the list by not providing an rg name parameter, I get the following error:

System.Private.CoreLib: Exception while executing function: Functions.getRg. System.Private.CoreLib: Result: Failure

Exception: System.AggregateException: One or more errors occurred. (The format of value 'text/plain, charset=utf-8' is invalid.)

What should the format be in the response header in this case? And am I doing it right?

The second behavior I am not understanding is also, when I try to get the id of one rg by using this line:

resJson = JsonSerializer.Serialize(resourceGroup.Data.Id);

I expect to get the id only, however I get a different large object.

Appreciate your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文