gpt4 book ai didi

c# - 列出 Azure 表值

转载 作者:行者123 更新时间:2023-12-03 01:23:55 24 4
gpt4 key购买 nike

我有一个问题,这可能很愚蠢,但我花了将近一周的时间。我想使用http触发器创建Azure函数,它将检查请求的URL是否在Azure表中,如果这样的url存在,它会将其重定向到另一个url,例如给定数据库记录中有 3 个值。重定向过程以及记录匹配时从数据库检索数据的过程我已经工作,唯一的问题是如何从给定行获取特定值。如何获取特性的值(value)?将“数据”转换为列表不起作用。

using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Microsoft.Azure.Cosmos.Table;
using System.Linq;
using static System.Environment;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net;
using System.Collections.Generic;

namespace Company.Function
{
public static class Redirect2
{
[FunctionName("Redirect2")]
public static async Task<IActionResult> GetAll(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "todo")] HttpRequest req,
[Table("tablename", Connection = "AzureWebJobsStorage")] CloudTable cloudTable,
ILogger log)
{
string OriginUrl = "test.domain.com";

TableQuery<TodoTable> query = new TableQuery<TodoTable>().Where(
TableQuery.CombineFilters(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, OriginUrl),
TableOperators.Or, TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, OriginUrl)));

var segment = cloudTable.ExecuteQuery(query);
var data = segment.Select(TodoExtensions.ToTodo);

return new OkObjectResult(data);

}
}

}

public class TodoTable : TableEntity
{
public string URL { get; set; }

public string wwwURL { get; set; }

public string ClientURL { get; set; }
}

public class Todo
{
public string URL { get; set; }

public string wwwURL { get; set; }

public string ClientURL { get; set; }
}

public static class TodoExtensions
{
public static TodoTable ToTable(this Todo todo)
{
return new TodoTable
{
PartitionKey = todo.URL,
RowKey = todo.wwwURL,
ClientURL = todo.URL
};
}

public static Todo ToTodo(this TodoTable todoTable)
{
return new Todo
{
URL = todoTable.PartitionKey,
wwwURL = todoTable.RowKey,
ClientURL = todoTable.URL
};
}
}

最佳答案

你搞砸了分区

 TableQuery<TodoTable> query = new TableQuery<TodoTable>().Where(
TableQuery.CombineFilters(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, OriginUrl),
TableOperators.Or, TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, OriginUrl)));

因此,在这里您尝试通过 rowKey 或partitionKey 进行匹配,这将是昂贵的,因为它需要遍历表存储中的所有数据。您真正想做的始终是查询单个分区。

所以我建议您将 PartitionKey 设置为“默认”。那么你总是查询单个分区。

如果我正确理解你的意思,让你的代码工作,你应该这样做

public static async Task<IActionResult> GetAll(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "todo")]
HttpRequest req,
[Table("tablename", Connection = "AzureWebJobsStorage")]
CloudTable cloudTable,
ILogger log)
{
string OriginUrl = req.Host.Value;

TableQuery<TodoTable> query = new TableQuery<TodoTable>().Where(
TableQuery.CombineFilters(
TableQuery.GenerateFilterCondition(nameof(TableEntity.PartitionKey), QueryComparisons.Equal, OriginUrl),
TableOperators.And,
TableQuery.GenerateFilterCondition(nameof(TableEntity.RowKey), QueryComparisons.Equal, OriginUrl)))
.Take(1);

var segment = cloudTable.ExecuteQuery(query);
var data = segment.Select(TodoExtensions.ToTodo).FirstOrDefault();

return new OkObjectResult(new { url = data?.URL});

}

关于c# - 列出 Azure 表值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67584684/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com