简体   繁体   English

c# .net core web api x-www-form-urlencoded parameter is always null

[英]c# .net core web api x-www-form-urlencoded parameter is always null

I have this method that looks a bit like this:我有一个看起来有点像这样的方法:

/// <summary>
/// Updates the status to paid
/// </summary>
/// <param name="data">The data from world pay</param>
/// <returns></returns>
[HttpPost]
[Consumes("application/x-www-form-urlencoded")]
public IActionResult Residential([FromForm] string data)
{
    if (string.IsNullOrEmpty(data)) return BadRequest("No data was present");

    var model = JsonConvert.DeserializeObject<WorldPayResponseModel>(data);

    // ----- removed from brevity ----- //

    return Ok(true);
}

When I use postman to send some data, it is always null.当我使用 postman 发送一些数据时,它总是 null。

在此处输入图像描述

Does anyone have any idea why this might be?有谁知道为什么会这样?

Create a model/DTO for the data being posted, .net core should handle the binding ( Model Binding in ASP.NET Core ).为要发布的数据创建模型/DTO,.net 核心应处理绑定( ASP.NET 核心中的 Model 绑定)。

You seem to already have WorldPayResponseModel , so why not bind to that?您似乎已经拥有WorldPayResponseModel ,那么为什么不绑定呢? For example:例如:

public class WorldPayResponseModel
{
    public string CardType { get; set; } 
    public int CardId { get; set; } 
    // other properties
    ...
}

[HttpPost]
[Consumes("application/x-www-form-urlencoded")]
public IActionResult Residential([FromForm] WorldPayResponseModel model)
{
    if (model == null || !ModelState.IsValid) return BadRequest("No data was present");    

    // ----- removed from brevity ----- //

    return Ok(true);
}

You can also add DataAnnotations to the properties, then in the controller you can use ModelState.IsValid .您还可以将 DataAnnotations 添加到属性中,然后在 controller 中您可以使用ModelState.IsValid Model validation in ASP.NET Core is a useful resource (it targets MVC/Razor pages, but model validation still works in an API). ASP.NET Core 中的 Model 验证是一个有用的资源(它针对 MVC/Razor 页面,但 model 验证仍然在 API 中工作)。

You can probably safely remove [FromForm] too.您也可以安全地删除[FromForm]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 application/x-www-form-urlencoded 内容类型处理请求,但 NET Core 中的 XML 主体 Web API - Handle Request with application/x-www-form-urlencoded content-type but XML body in NET Core Web API Web API和应用程序/ x-www-form-urlencoded JSON - Web API and application/x-www-form-urlencoded JSON 在 Minimal API NET 6 中接受 x-www-form-urlencoded - Accept x-www-form-urlencoded in Minimal API NET 6 ASP.NET web api 无法获取 application/x-www-form-urlencoded HTTP POST - ASP.NET web api cannot get application/x-www-form-urlencoded HTTP POST 带有 application/x-www-form-urlencoded HTTP POST 的 ASP.NET web api 映射模型 - ASP.NET web api mapping model with application/x-www-form-urlencoded HTTP POST C# .net core web api post参数始终为空 - C# .net core web api post parameter always null POST到内容类型为x-www-form-urlencoded的asp.net Web API会引发错误415不支持的媒体类型 - POST to asp.net web api with content type x-www-form-urlencoded raises error 415 unsupported media type 使用x-www-form-urlencoded发布数据的ASP.Net Web API自定义模型绑定 - 似乎没什么用 - ASP.Net Web API custom model binding with x-www-form-urlencoded posted data - nothing seems to work ASP.NET Core提取POST FormData()应用程序/ x-www-form-urlencoded - ASP.NET Core Fetch POST FormData() application/x-www-form-urlencoded 标题:在 asp.net 内核中将数据发布为 application/x-www-form-urlencoded - Title :post data as application/x-www-form-urlencoded in asp.net core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM