【问题标题】:Cant find method: Using MVC httppost to add rows to azure table storage找不到方法:使用 MVC httppost 将行添加到 azure 表存储
【发布时间】:2017-03-23 07:06:51
【问题描述】:

我正在尝试向天蓝色表存储添加一行。这是我第一次使用这个,所以我遵循了一个 MS 教程,不幸的是他们使用控制台应用程序而不是 MVC,所以我不得不即兴发挥一点,基本上,当我将 [httpPost] 添加到我的 AddRow 方法时,我得到了一个 http 404 错误 “'/' 应用程序中的服务器错误。找不到资源。”

代码(在 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

访问它

【问题讨论】:

    标签: asp.net-mvc azure asp.net-mvc-5 azure-table-storage


    【解决方案1】:

    当我们在浏览器中发出请求时,默认的 HttpMethod 是 HttpGet。它与函数中定义的 HttpMethod (Httppost) 不匹配。因此,它将返回上述错误。我们通常使用 post 方法来获取表单数据,然后我们可以在视图页面中定义 httpMethod。

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

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

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

    【讨论】:

      猜你喜欢
      • 2012-08-15
      • 2019-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多