找不到方法:使用 MVC httppost 将行添加到 azure table 存储

Cant find method: Using MVC httppost to add rows to azure table storage

我正在尝试向天蓝色 table 存储添加一行。这是我第一次使用它,所以我遵循了 MS 教程,不幸的是他们使用控制台应用程序而不是 MVC,所以我不得不即兴发挥一点,基本上,在我的 AddRow 方法中添加 [httpPost]我收到 http 404 错误 "Server Error in '/' Application.The resource cannot be found. "

代码(在 HomeController 内部):

[HttpPost]
        public ActionResult AddRow()
        {
            ViewBag.Message = "Your application description page.";

            try
            {
                CloudStorageAccount cloudStorageAcc = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));

                CloudTableClient cloudTableClient = cloudStorageAcc.CreateCloudTableClient();

                CloudTable table = cloudTableClient.GetTableReference("Users");
                table.CreateIfNotExists();

                Insert inserter1 = new Insert("Bob", "Contoso");
                inserter1.UserName = "Bobbie";

                TableOperation insertOperation = TableOperation.Insert(inserter1);

                table.Execute(insertOperation);
            }
            catch
            {
                return View();
            }

            return View();
        }

我尝试通过 mysite.azurewebsites.net/Home/AddRow

访问它

当我们在浏览器中发起请求时,默认的HttpMethod是HttpGet。它与函数中定义的 HttpMethod (Httppost) 不匹配。所以,就会出现return以上的错误。我们通常使用post方法来提交表单数据,然后我们可以在视图页面中定义httpMethod。

@using (Html.BeginForm("AddRow", "Home", FormMethod.Post))
{
   // data or logic
}

我们也可以使用代码或工具(postman,fidder)来调用定义好的HttpMethod函数。

您似乎为每个 http 请求为 [partitionkeyrowkey] 设置了相同的值。如果是这样,它才第一次可以正常工作。因为 [partitionkeyrowkey] 对于 tableEntity(record) 是唯一的。更详细的请参考document