C# 列出Azure表值
我有一个问题,可能很愚蠢,但我花了将近一个星期的时间。我想创建带有http触发器的Azure函数,该函数将检查请求出现的URL是否在Azure表中,如果该URL存在,它将重定向到另一个URL,例如,给定db记录中的3个值。重定向过程和当记录与我的工作相匹配时从数据库检索数据的过程,唯一的问题是如何从给定行获取特定值。 如何获取属性的值?将“数据”转换为列表不起作用C# 列出Azure表值,c#,azure,azure-functions,azure-table-storage,C#,Azure,Azure Functions,Azure Table Storage,我有一个问题,可能很愚蠢,但我花了将近一个星期的时间。我想创建带有http触发器的Azure函数,该函数将检查请求出现的URL是否在Azure表中,如果该URL存在,它将重定向到另一个URL,例如,给定db记录中的3个值。重定向过程和当记录与我的工作相匹配时从数据库检索数据的过程,唯一的问题是如何从给定行获取特定值。 如何获取属性的值?将“数据”转换为列表不起作用 using System.IO; using System.Threading.Tasks; using Microsoft.Asp
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
};
}
}
使用System.IO;
使用System.Threading.Tasks;
使用Microsoft.AspNetCore.Mvc;
使用Microsoft.Azure.WebJobs;
使用Microsoft.Azure.WebJobs.Extensions.Http;
使用Microsoft.AspNetCore.Http;
使用Microsoft.Extensions.Logging;
使用Newtonsoft.Json;
使用Microsoft.Azure.Cosmos.Table;
使用System.Linq;
使用静态系统。环境;
使用System.Net.Http;
使用System.Net.Http.Header;
Net系统;
使用System.Collections.Generic;
名称空间Company.Function
{
公共静态类重定向2
{
[函数名(“重定向2”)]
公共静态异步任务GetAll(
[HttpTrigger(AuthorizationLevel.Function,“get”,Route=“todo”)]HttpRequest请求,
[Table(“tablename”,Connection=“azurewebjobstorage”)]CloudTable CloudTable,
ILogger日志)
{
string OriginUrl=“test.domain.com”;
TableQuery=新建TableQuery()。其中(
TableQuery.CombineFilters(TableQuery.GenerateFilterCondition(“PartitionKey”,QueryComparisons.Equal,OriginUrl),
TableOperators.Or,TableQuery.GenerateFilterCondition(“RowKey”,QueryComparisons.Equal,OriginUrl));
var段=cloudTable.ExecuteQuery(查询);
var数据=段.Select(TodoExtensions.tototodo);
返回新的OkObjectResult(数据);
}
}
}
公共类ToToToTable:TableEntity
{
公共字符串URL{get;set;}
公共字符串wwurl{get;set;}
公共字符串ClientURL{get;set;}
}
公共课待办事项
{
公共字符串URL{get;set;}
公共字符串wwurl{get;set;}
公共字符串ClientURL{get;set;}
}
公共静态类TodoExtensions
{
公共静态Todo ToTable ToTable(此Todo Todo)
{
返回新的ToToToTable
{
PartitionKey=todo.URL,
RowKey=todo.wwurl,
ClientURL=todo.URL
};
}
公共静态Todo ToTodo(此TodoTable TodoTable)
{
返回新待办事项
{
URL=ToToToTable.PartitionKey,
wwurl=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)));
TableQuery query=new TableQuery()。其中(
TableQuery.CombineFilters(TableQuery.GenerateFilterCondition(“PartitionKey”,QueryComparisons.Equal,OriginUrl),
TableOperators.Or,TableQuery.GenerateFilterCondition(“RowKey”,QueryComparisons.Equal,OriginUrl));
因此,在这里,您尝试按rowKey或partitionKey进行匹配,这将非常昂贵,因为它需要通过表存储中的所有数据进行匹配。您真正想要做的是始终查询单个分区
所以我建议您将PartitionKey设置为“Default”。然后您总是查询单个分区
如果我正确理解你的代码,你应该这样做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});
}
公共静态异步任务GetAll(
[HttpTrigger(AuthorizationLevel.Function,“get”,Route=“todo”)]
HttpRequest请求,
[表(“tablename”,Connection=“azurewebjobstorage”)]
CloudTable CloudTable,
ILogger日志)
{
字符串OriginUrl=req.Host.Value;
TableQuery=新建TableQuery()。其中(
TableQuery.CombineFilters(
TableQuery.GenerateFilterCondition(nameof(TableEntity.PartitionKey)、QueryComparisons.Equal、OriginUrl),
表3.以及,
TableQuery.GenerateFilterCondition(nameof(TableEntity.RowKey),QueryComparisons.Equal,OriginUrl)))
.采取(1)项措施;
var段=cloudTable.ExecuteQuery(查询);
var data=segment.Select(TodoExtensions.tototodo.FirstOrDefault();
返回新的OkObjectResult(新的{url=data?.url});
}
您希望url具有单一匹配项吗?如何通过url将原始url传递给您的服务?@VovaBilyachat我将使用dns将url重定向到应用程序服务。是的,如果有记录,那么只有一条。逻辑将更像这样->浏览器中的url重定向到appservice的地址,该函数检查url是否在数据库中->如果是,它将用户重定向到url,该url将是此记录的3个值so originUrl是您将从域中获得的域?req.Host.Value?不是这样的。来自存储资源管理器的数据。打开表并显示数据作为我获得所有3个值(partitionkey、rowkey、URL)的结果,如何在它们之后枚举,以便仅提取我感兴趣的值-仅值3列?如何分割“数据”变量?因为你搞乱了分区键。你能用数据在问题中添加截图吗?您可能对所有的分区都有相同的分区键them@Kivas检查代码我更新了itI我花了一周时间做了一件只需要一个小“?”的东西。就这一刻而言,我希望当我把所有的部分放在一起时,它也会起作用。多谢各位@VovaBilyachat@Kivas通常是这样的;)这些耗时的问题通常是非常愚蠢的错误,这就是为什么如果我不能在45分钟内解决问题,我会寻求帮助