【问题标题】:Sending data in application/x-www-form-urlencoded format from postman to Web API Controller以 application/x-www-form-urlencoded 格式从邮递员发送数据到 Web API 控制器
【发布时间】:2021-08-07 03:19:28
【问题描述】:

我正在从邮递员向 Web api 控制器发送一些数据,但它没有命中我错过了什么吗?

下面是我的代码。

[HttpPost]
[Route("[controller]")]
[Consumes("application/x-www-form-urlencoded")]
public ActionResult<Root> PostData([FromBody] Root inputData)
{
    //StringBuilder sb = new StringBuilder();
    //sb.Append("q:" + inputData.q + " siteKey:" + inputData.siteKEY + " siteCredentials:" + inputData.siteCredential + " user:" + inputData.userID + " patient:" + inputData.patientID + " uuid:" + inputData.UUID);
    return Ok(inputData); //(sb.ToString());
}

【问题讨论】:

  • 您看到了什么错误? 404?
  • 405 方法不允许
  • 你能显示 Root 类吗?
  • 我可能只是在这里吐个球,但是如果您使用的是启动程序时自动生成的 Weatherforecast 控制器,您的 api 控制器本身将具有Route("[Controller]") 标签。因此,如果您还在端点上方放置另一个标签,那么您尝试查找的 URL 应该是:host:port/WeatherForecast/WeatherForecast。如果这不是问题,请尝试将 [FromBody] 更改为 [FromForm]
  • 公共类 Root { public Args args { get;放; } 公共字符串数据 { 获取;放; } 公共文件文件 { 获取;放; } 公共表格形式 { 得到;放; } 公共标头 标头 { 获取;放; } 公共 Json json { 获取;放; } 公共字符串 url { 获取;放; } }

标签: c# asp.net-mvc asp.net-core asp.net-web-api asp.net-web-api2


【解决方案1】:

[FromForm] 属性用于从内容类型application/x-www-url-formencoded 发送的提交表单的传入数据,而[FromBody] 将以默认方式解析模型,在大多数情况下由内容类型application/json 发送, 来自请求正文。

【讨论】:

    【解决方案2】:

    application/x-www-form-urlencoded 或多或少与 URL 末尾的查询字符串相同。所以删除

    [Consumes("application/x-www-form-urlencoded")]
    

    在使用 Postman 时,您最好使用 raw 和 json 创建输入参数

    【讨论】:

      【解决方案3】:

      尝试在 Postman 设置中关闭“ssl 证书验证” 只需转到设置 -> 常规 -> 关闭 ssl 证书验证

      【讨论】:

        【解决方案4】:
        [HttpPost]
        [Route("[api/Home/PostData]")]                   //check here please specify controllername and index name
        [Consumes("application/x-www-form-urlencoded")]
        public ActionResult<Root> PostData([FromBody] Root inputData)
        {
            //StringBuilder sb = new StringBuilder();
            //sb.Append("q:" + inputData.q + " siteKey:" + inputData.siteKEY + " siteCredentials:" + inputData.siteCredential + " user:" + inputData.userID + " patient:" + inputData.patientID + " uuid:" + inputData.UUID);
            return Ok(inputData); //(sb.ToString());
        }
        
        

        添加控制器名称和索引名称

        [Route("[api/Home/PostData]")]                   //check here please specify controllername and index name
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-05-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-21
          • 1970-01-01
          • 1970-01-01
          • 2014-04-16
          相关资源
          最近更新 更多