控制器动作不会以HttpPut的形式执行,但会以HttpPost的形式执行

时间:2019-07-05 19:54:48

标签: c# rest asp.net-core-mvc

我正在创建一个与Web API交互的简单小网站,允许用户拥有emus数据库。我目前正在控制器中进行PUT操作。投放优先权来自仅重定向到新视图的操作。然后,该视图提示用户输入信息,并在模型中返回该信息。然后,第二步应处理该信息并调用API。

第一个动作和第二个动作具有相同的名称,因此处理API请求的第二个动作顶部带有[HttpPut]标记。但是,如果它具有该标记,则视图将永远不会进入第二个Action,它只会在我按Submit时重新加载页面。但是,如果我输入[HttpPost],效果很好。

我已经尝试更改IIS Express中的配置,但无济于事。

以下是该视图的代码:

@model HelloEmuWebsite.Models.EmuItem
@using (Html.BeginForm())
{

    <div>
        @Html.LabelFor(x => x.SearchName)
        @Html.TextBoxFor(x => x.SearchName)
    </div>
    <div>
        @Html.LabelFor(x => x.Name)
        @Html.TextBoxFor(x => x.Name)
    </div>
    <div>
        @Html.LabelFor(x => x.Age)
        @Html.TextBoxFor(x => x.Age)
    </div>
    <div>
        @Html.LabelFor(x => x.Weight)
        @Html.TextBoxFor(x => x.Weight)
    </div>

    <input type="submit" value="OK" />


}

这是控制器中的代码:

public IActionResult ChangeOneEmu()
        {
            return View(new EmuItem());   
        }

        [HttpPut]
        async public Task<IActionResult> ChangeOneEmu(EmuItem model)
        {
            var baseAddr = new Uri("my_url/api/values/");
            var client = new HttpClient { BaseAddress = baseAddr };
            var response = await client.PutAsJsonAsync(model.SearchName, model);
            return RedirectToAction("Index");
        }

这是我的自定义API中PUT请求的代码:

 [HttpPut("{id}")]
        public ActionResult<List<Models.EmuItem>> Put(string id, [FromBody] Models.EmuItem ChangedEmu)
        {
            if (ModelState.IsValid)
            {
                return MyEmus.ChangeEmu(id, ChangedEmu);

            }

            return BadRequest(ModelState);
        }

为什么会这样?我该如何解决它以便接受[HttpPut]?另外,是否有一种更好的方法可以不依赖[http]标签呢? 谢谢!

1 个答案:

答案 0 :(得分:1)

浏览器仅支持GET和POST发送表单。 method属性的唯一有效值是get和post,分别对应于GET和POST HTTP方法。 create or replace function hunts_maintain() returns trigger as $hunts_maintain$ begin if (tg_op = 'INSERT') then insert into hunts_summary(id_h, nhunts) values (NEW.id_h, 1); elsif (tg_op = 'DELETE') then insert into hunts_summary(id_h, nhunts) values (OLD.id_h, -1); elsif (tg_op = 'UPDATE' and NEW.id_h!=OLD.id_h) then insert into hunts_summary(id_h, nhunts) values (OLD.id_h, -1), (NEW.id_h, 1); end if; if (random()*1024 < 1) then with deleted_ids as ( select id_hs from hunts_summary for update skip locked ), deleted_nhunts as ( delete from hunts_summary where id_hs in (select id_hs from deleted_ids) returning id_h, nhunts ) insert into hunts_summary (id_h, nhunts) select id_h, sum(nhunts) from deleted_nhunts group by id_h; end if; return NEW; end; $hunts_maintain$ language plpgsql; create trigger hunts_maintain after insert or update or delete on hunts for each row execute procedure hunts_maintain(); 是无效的HTML,将被视为create or replace view hunts_summary_view as select id_h, sum(nhunts) as nhunts from hunts_summary group by id_h; ,即发送GET请求。您可以使用AJAX将Sub test Dim LR As Long LR = Range("D" & Rows.Count).End(xlUp).Row Range("F4:F" & LR).Formula = “=d4*-1” Range("F4:F" & LR).Copy Range("F4:F" & LR).PasteSpecial xlPasteValues End sub 请求发送到<form method="put">

您可以为<form>PUT元素分配Controller,然后像这样发送模型数据:

id