返回资源组对象并列表< string>使用Azure功能C#
我正在创建一个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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论