You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CosmosDB中已废弃UriFactory.CreateDocumentUri的替代方案与文档URI获取咨询

替代废弃的UriFactory.CreateDocumentUri的方案

不用手动生成,Azure Cosmos DB SDK v3及以上版本已经提供了更简洁的官方替代方案,分两种场景处理:

  • 从Upsert返回结果直接获取
    调用UpsertItemAsync后,返回的ItemResponse<T>对象里的文档实体(response.Resource)自带_self属性,这就是该文档的完整URI,直接取用即可——这是最可靠的方式,因为是Cosmos DB自动维护的,不会出现格式或编码错误:
var upsertResponse = await yourContainer.UpsertItemAsync(yourDocument);
string documentUri = upsertResponse.Resource._self;
  • 手动构造文档URI
    如果没拿到Upsert的返回对象,需要手动构造的话,用Container.GetItemUri(id, partitionKey)方法,这是官方明确推荐用来替代UriFactory.CreateDocumentUri的API:
string docUri = yourContainer.GetItemUri(documentId, partitionKeyValue).ToString();

尽量避免自己拼接URI,很容易因为路径格式、特殊字符编码等问题出错,优先使用上面两种官方方式。

内容的提问来源于stack exchange,提问作者RheinmetallSkorpion

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 14:30:55