【问题标题】:ASP.NET MVC Routing with Model Parameter带有模型参数的 ASP.NET MVC 路由
【发布时间】:2016-10-02 06:51:24
【问题描述】:

我在通过 Ajax 调用的控制器中有一个操作方法。规范是:

    [HttpPost]
    public ActionResult SomeFunction(FormCollection form)

无需任何特殊的路由配置即可正常工作。

但是,如果我尝试使用模型而不是 FormCollection 作为参数,那么:

    [HttpPost]
    public ActionResult SomeFunction(SomeModel model)

我收到 500 错误,因为找不到该函数。

我需要在 RouteConfig 中添加什么才能使其正常工作?

【问题讨论】:

  • A 500(Internal Server Error) 是因为你的方法抛出了异常(和没找到没关系)
  • 噢!你说得对。只是我的模型没有默认构造函数。

标签: asp.net-mvc asp.net-mvc-5 asp.net-mvc-routing model-binding


【解决方案1】:

我建议使用帮助插件:http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/creating-api-help-pages

当使用 POST 时,我会这样指定签名:

#region POST
[Route("")]
public IHttpActionResult Post([FromBody]Contact newContact)

【讨论】:

    猜你喜欢
    • 2017-01-03
    • 2023-03-30
    • 2018-06-04
    • 2014-10-13
    • 2011-04-08
    • 1970-01-01
    • 2017-11-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多