列出Azure表值



我有一个问题,这个问题可能很愚蠢,但我花了将近一周的时间。我想创建一个带有http触发器的Azure函数,它会检查请求的URL是否在Azure表中,如果存在这样的URL,它会将其重定向到另一个URL,例如,给定数据库记录中的3个值。重定向过程和当记录匹配时从数据库检索数据的过程我一直在工作,唯一的问题是如何从给定的行中获得特定的值。如何获取属性的价值?将"数据"转换为列表无效。

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设置为";默认值";。然后您总是查询单个分区。

如果我正确理解你,使你的代码工作,你应该做smth像这个

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});
}

相关内容

  • 没有找到相关文章

最新更新