ASP.NET Core POST执行错误的操作

时间:2019-05-02 13:51:21

标签: asp.net-core routing

我有一个执行以下操作的控制器

[HttpPost("training/create", Name="Create")]
public async Task<IActionResult> Create(CreateTrainingCommand command){
    // doing stuff ...
}

[HttpGet("training/createblankform", Name="CreateBlankForm")]
public async Task<IActionResult> CreateBlankForm(CreateBlankFormQuery command){
    // doing stuff ...
}

[HttpPost("training/createblankform", Name="CreateBlankForm")]
public async Task<IActionResult> CreateBlankForm(CreateBlankFormCommand command){
    // doing stuff ...
}

在StartUp.cs文件中,我正在使用app.UseMvcWithDefaultRoute() and I have loaded a View that is loaded using the CreateBlankForm GET`操作。

该视图包含应提交给CreateBlankForm POST动作的表单,但Create POST动作受到了打击。

我尝试以几种不同的方式配置表单;

<form class="form-horizontal" role="form" contenteditable="" asp-controller="Training" asp-action="CreateBlankForm" method="post">
...
        <input id="submit" type="button" value="Create" />
</form>

<form class="form-horizontal" role="form" contenteditable="" asp-route="CreateBlankTrainingForm" method="post">
...
        <input id="submit" type="button" value="Create" />
</form>

<form class="form-horizontal" role="form" action="/training/createblankform" method="post">
...
        <input id="submit" type="button" value="Create" />
</form>

但是所有这些仍然击中了CreateBlankForm POST动作。为什么这会打错控制器?

2 个答案:

答案 0 :(得分:1)

是否有任何JavaScript事件正在处理submit表单操作?

答案 1 :(得分:0)

尝试更改

    <input id="submit" type="button" value="Create" />

    <input id="submit" type="submit" value="Create" />

以及您指定的错误操作名称 应该是

action="createblankform"

不是

action="/training/createblankform"

如果您有任何问题,请告诉我