c# - 如何从 Razor Page 获取 Azure 数据表 ETag 值

标签 c# azure razor razor-pages azure-table-storage

我使用 Azure.Data.Table NuGet 包进行 Azure 表存储。我想从 Razor Page 获取 ETag 属性。它作为 {} 到达 Controller 。

示例代码如下。

    [HttpPost]
    public async Task<IActionResult> Update(Product product)
    {
        await _tableStorageService.UpdateAsync(product, product.ETag);
        return RedirectToAction("Index");
    }

enter image description here

和cshtml

@model AzureStorage.Library.Models.Product
@{
    ViewData["Title"] = "Index";
}

<h2>Add or Update Product</h2>
<form asp-action="@(ViewBag.IsUpdate ? "Update" : "Create")" method="post" enctype="application/x-www-form-urlencoded">
    <div class="form-group mb-2 d-flex" style="column-gap:10px;">
        <input type="hidden" asp-for="RowKey" />
        <input type="hidden" asp-for="PartitionKey" />
        <input type="hidden" asp-for="ETag" />
    
        <input class="form-control mr-1" asp-for="Name" placeholder="Product Name" />
        <input class="form-control mr-1" asp-for="Price" placeholder="Product Price" />
        <input class="form-control mr-1" asp-for="Stock" placeholder="Product Stock" />

        <input class="btn btn-primary" type="submit" value="@(ViewBag.IsUpdate ? "Update Product" : "Add Product")" />
    </div>
</form>

如何获取 ETag 值?

最佳答案

我们可以从 blob 的属性中获取 E-tag 的值。

在 azure 中创建存储和容器,如下所示。

enter image description here

使用 C# 代码获取 E-tag 值。

enter image description here

enter image description here

string storageAccount_connectionString ="Connection String";
CloudStorageAccount storage_Account = CloudStorageAccount.Parse(storageAccount_connectionString);
CloudBlobClient blob_Client = storage_Account.CreateCloudBlobClient();
CloudBlobContainer container = 
blob_Client.GetContainerReference(containerName);
container.CreateIfNotExists();

CloudBlockBlob block_Blob = container.GetBlockBlobReference(blobName);
block_Blob.UploadText("Sample Text...");

string originalETag = block_Blob.Properties.ETag;
Console.WriteLine("ETag = {0}", originalETag);

关于c# - 如何从 Razor Page 获取 Azure 数据表 ETag 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74668115/

相关文章:

c# - 显示来自 azure blob 容器的图像

c# - 授权 USB 驱动器

c# - 调用/创建委托(delegate)

c# - 如何在 C# 中模拟 Microsoft Excel 的规划求解功能(GRG 非线性)?

Azure Web 应用程序 session 和 ARR

Azure 数据工厂 V2 : Create linked service for Azure Sql server using Azure Active Directory

validation - 如果验证失败则显示 div

javascript - 返回带有验证错误的 Ajax.BeginForm 时隐藏按钮。如何?

css - 如何让 PagedList.Pagecount 和 @Html.PagedListPager 显示在同一行?

c# - 从 lambda 表达式参数读取和写入模型属性