【问题标题】:Asp.net Core Migration, Post actions no longer workAsp.net Core 迁移,发布操作不再起作用
【发布时间】:2018-12-14 10:55:13
【问题描述】:

我已将一个 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})"] 那么它会导航到该操作,但模型为空。我是否缺少其他配置?

【问题讨论】:

    标签: c# razor asp.net-core-mvc asp.net-core-2.0


    【解决方案1】:

    您的路线好像有错字,端点不会被命中。

    [Route("Convert/Add/{entryViewModel}")]
    

    应该是

    [Route("Concert/Add/{entryViewModel}")]
    

    我还将删除@Html.BeginForm 中的new { eventId = Model.EventId },以确保EntryViewModel 被序列化并正确传递到HTTP 端点。

    另外,由于您没有提供 EntryViewModel 类,我会确保它具有正确的关联 getter 和 setter,以便模型绑定起作用,例如:

    public class EntryViewModel
    {
       [Required]
       [DisplayName(Name="Forename")]
       public string Forename { get; set; }
    }
    

    在您的表单中,您可以使用ASP.NET Core Tag Helpers.

    <form asp-controller="Concert" asp-action="Add" method="post">
        Forename:  <input asp-for="Forename" /> 
        <br />
        <button type="submit">Submit</button>
    </form>
    

    【讨论】:

    • 我的错,这是一个复制粘贴到堆栈中的错误。我已经更新了。
    【解决方案2】:

    使用 [FromBody] 作为参数

     [HttpPost]
     [Route("Concert/Add")]
     public IActionResult Add([FromForm]EntryViewModel entryViewModel)
     {
    
     }
    

    我也看到了:

    new { eventId = Model.EventId }
    

    这样更好

     [HttpPost]
     [Route("Concert/Add/{eventId:int}")]
     public IActionResult Add(int eventId,[FromForm]EntryViewModel entryViewModel)
     {
    
     }
    

    【讨论】:

    • 还是不喜欢 :(。是属性路由吗?需要“开启”吗?
    • 你能从inspector那里得到http请求,看看你的请求url和数据是如何形成的吗?
    • 或更多功能。从 Http.BeginForm 中删除新的 { eventId = Model.EventId }。然后 [Route("Concert/Add")] 和 public IActionResult Add([FromBody]EntryViewModel entryViewModel)
    • 这让我更接近了。看起来我需要使用 [FromForm] 而不是 [FromBody]
    • 是的。我使用角度并使用[FromBody]。我的错。更新答案。
    猜你喜欢
    • 2021-07-19
    • 1970-01-01
    • 1970-01-01
    • 2018-05-03
    • 2016-09-19
    • 1970-01-01
    • 1970-01-01
    • 2014-04-04
    • 2018-06-08
    相关资源
    最近更新 更多