将表单数据发布到MVC Core API [英] Posting form data to MVC Core API

查看:61
本文介绍了将表单数据发布到MVC Core API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用AJAX将数据发布到我的API,但是遇到问题.我正在使用Fiddler测试我的API,并且能够正确发布JSON,但是在发布名称/值urlencoded字符串时,我收到400错误的请求,响应主体为'{":[输入无效."]'.

I would like to post data to my API using AJAX but I'm having issues. I'm using Fiddler to test my API and I'm able to post JSON correctly but when posting a name/value urlencoded string I get a 400 Bad Request with the response body being '{"":["The input was not valid."]}'.

我的调试窗口显示:Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor:Information: Executing ObjectResult, writing value of type 'Microsoft.AspNetCore.Mvc.SerializableError'.

要发布的JSON是:

{
    "Name": "Test"
}

要发布的表单数据为:

Name=Test

这是控制器和操作:

[Route("api/[Controller]")]
[ApiController]
public class AccountsController : Controller
{
    [HttpPost]
    public IActionResult CreateAccount(Account account)
    {
        //code
    }
}

这是帐户"类别:

public class Account
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string Type { get; set; }
    public string Website { get; set; }
}

很明显,在模型绑定期间存在问题,但是表单数据似乎有效(我也使用AJAX生成了表单数据,并且也得到了400).

It seems obvious that there is an issue during model binding but the form data seems valid (I've also generated form data using AJAX and get a 400 as well).

推荐答案

在他的帖子中 ,安德鲁·洛克(Andrew Lock)解释说,要在ASP.NET Core中绑定JSON POST,必须在参数上指定[FromBody]属性,就像这样:

In his post Model binding JSON POSTs in ASP.NET Core from 2016, Andrew Lock explains that in order to bind a JSON POST in ASP.NET Core, the [FromBody] attribute must be specified on the argument, like so:

[HttpPost]
public IActionResult CreateAccount([FromBody] Account account)
{
    // ...
}

随着[ApiController]的ASP.NET Core 2.1引入,不再需要此功能.这里重要的是,当绑定的类型为复杂"时(在您的示例中),此属性有效地推断[FromBody]属性的存在.换句话说,就好像您已经编写了我上面演示的代码一样.

With the ASP.NET Core 2.1 introduction of [ApiController], this is no longer required. Of importance here is that this attribute effectively infers the presence of the [FromBody] attribute when the type being bound is "complex" (which it is in your example). In other words, it's as though you have written the code as I demonstrated above.

安德鲁(Andrew)在其帖子中还指出以下内容:

In his post, Andrew also states the following:

在某些情况下,您可能需要能够将两种类型的数据绑定到一个动作.在这种情况下,您可能会有点卡住,因为不可能使同一个端点接收两组不同的数据.

In some cases you may need to be able to bind both types of data to an action. In that case, you're a little bit stuck, as it won't be possible to have the same end point receive two different sets of data.

在这里,当提到两种数据类型时,Andrew既指的是JSON帖子,也指的是基于表单的POST.他继续解释如何实际实现所需的结果.根据您的目的修改他的示例,您需要执行以下操作:

Here, when referring to both types of data, Andrew is referring to both a JSON post and a form-based POST. He continues on to explain how to actually achieve the required result. Modifying his example for your purposes, you'd need to do something like the following:

// Form.
[HttpPost("FromForm")]
public IActionResult CreateAccountFromForm([FromForm] Account account)) =>
    DoSomething(account);

// JSON.
[HttpPost("FromBody")]
public IActionResult CreateAccountFromBody(Account account) =>
    DoSomething(account);

private IActionResult DoSomething(Account account) {
    // ...
}

在安德鲁(Andrew)的示例中,[FromBody]是显式的,而[FromForm]是隐式的,但是鉴于[ApiController]对默认值的影响,上述修改后的示例将其翻转.

In Andrew's example, the [FromBody] is explicit and the [FromForm] is implicit, but given the affect that [ApiController] has on the defaults, the modified example above flips that around.

此处中查看我的答案,以获取一种可能的方法,该方法允许将相同的URL用于FromFormFromBody使用自定义IActionConstraint.

See my answer here for a potential approach that allows for the same URL to be used for both FromForm and FromBody using a custom IActionConstraint.

这篇关于将表单数据发布到MVC Core API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆