我已将一个 Asp.Net 项目移植到 .Net Core,并注意到我的 POST 端点不再工作。
[HttpGet, Route("Concert/Add/{eventId:int?}")]
public ActionResult Add(int eventId)
{
//This works
}
[HttpPost]
[Route("Concert/Add")]
public IActionResult Add(EntryViewModel entryViewModel)
{
//This action is never reached. I get a 404 Not found in browser
}
在我看来,我有以下形式:
@using (Html.BeginForm("Add", "Concert", new { eventId = Model.EventId }, FormMethod.Post, null, new { @class = "center-block entryform AddEntry" }))
{
<div class="form-group">
@Html.LabelFor(model => model.Forename, new { @class = "control-label entryLabel" })
<div class="">
@Html.TextBoxFor(model => model.Forename, new { @class = "form-control" })
</div>
</div>
}
我的 StartUp.cs Configure() 看起来像这样:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
routes.MapRoute(
name: "Events",
template: "{controller=Home}/{action=Index}/{eventId?}");
});
如果我将我的 Post 端点路由更改为 [Route("Customer/Add/{entryViewModel})"] 那么它导航到该操作,但模型为空。我是否缺少其他配置?