【问题标题】:Web API parameter is always nullWeb API 参数始终为空
【发布时间】:2015-06-25 12:50:09
【问题描述】:

我的 Web API 中有以下方法:

[AcceptVerbs("POST")]
public bool MoveFile([FromBody] FileUserModel model)
{
    if (model.domain == "abc")
    {
        return true;
    }
    return false;
}

FileUserModel 定义为:

public class FileUserModel
{
    public string domain { get; set; }
    public string username { get; set; }
    public string password { get; set; }
    public string fileName { get; set; }
}

我试图通过 Fiddler 调用它,但每当我这样做时,模型总是设置为 null。在 Fiddler 中,我已将作曲家发送给使用 POST,该 url 在那里并且正确,因为 Visual Studio 中的调试器在调用时会中断。我设置的请求为:

User-Agent: Fiddler 
Host: localhost:46992 
Content-Length: 127 
Content-Type: application/json

请求正文为:

"{
  "domain": "dcas"
  "username": "sample string 2",
  "password": "sample string 3",
  "fileName": "sample string 4"
}"

但每当我在调试器遇到断点时运行作曲家时,它总是显示模型为空。

【问题讨论】:

  • 请求正文是否有您在此处发布的双引号?
  • 这是一个错字,在 domain: dcas 之后没有“,”?
  • 您是否尝试将一项测试的输入参数类型从 FileUserModel 更改为字符串?
  • 尝试我在以下答案中提供的解决方案
  • 那么你需要删除那些引号,它们是不需要的。

标签: c# asp.net json asp.net-web-api fiddler


【解决方案1】:

您在发送的请求中缺少,。此外,由于包含双引号,您实际上发送的是 JSON 字符串,而不是 JSON 对象。删除引号并添加逗号应该可以解决您的问题。

{
    "domain": "dcas", // << here
    "username": "sample string 2",
    "password": "sample string 3",
    "fileName": "sample string 4"
}

此外,由于您发布的是模型,因此您不需要 [FromBody] 属性。

[AcceptVerbs("POST")]
public bool MoveFile(FileUserModel model)
{
    if (model.domain == "abc")
    {
        return true;
    }
    return false;
}

应该没问题。有关这方面的更多信息,请参阅this blog

【讨论】:

  • 这是多余的,但没有回答问题。
  • 编辑也是不正确的,这个上下文中的“JSON字符串”是什么?加上引号,数据对 API 来说毫无意义。
  • 编辑实际上给出了正确的答案。 API 会将请求解释为 JSON 字符串,因为 Content-Type(根据 OP 的帖子)是 application/json
【解决方案2】:

您遇到的问题是您发布带有双引号的 JSON 数据。删除它们,它应该可以工作。

同时修复缺少的逗号:

{
  "domain": "dcas",
  "username": "sample string 2",
  "password": "sample string 3",
  "fileName": "sample string 4"
}

【讨论】:

    【解决方案3】:

    你需要像下面这样进行ajax调用

    $(function () {
        var FileUserModel =    { domain: "dcas", username: "sample string 2", 
                password: "sample string 3", fileName: "sample string 4"};
        $.ajax({
            type: "POST",
            data :JSON.stringify(FileUserModel ),
            url: "api/MoveFile",
            contentType: "application/json"
        });
    });
    

    不要忘记将内容类型标记为 json 和服务器端 api 代码

    [HttpPost]
    public bool MoveFile([FromBody] FileUserModel model)
    {
        if (model.domain == "abc")
        {
            return true;
        }
        return false;
    }
    

    Sending HTML Form Data in ASP.NET Web API

    【讨论】:

    • @DavidG - 我更正了我的代码中的那部分......并且还建议他需要检查一些事情......我认为当给出错误的解决方案时给出-1,而不是正确的解决方案。 . 有什么问题我建议使用 JSON.stringify 并要求他检查 contentType: "application/json".. 我猜这没什么问题
    • @DavidG - 这并不意味着给 -1 ...我用 jquery 完整代码指向所有东西...这有什么害处...如果您对放 -​​1 不满意问题..如果您谈论在我的答案中更新的错误 json...
    猜你喜欢
    • 2013-01-15
    • 2017-08-09
    • 2014-08-06
    • 1970-01-01
    • 1970-01-01
    • 2017-08-28
    • 1970-01-01
    • 1970-01-01
    • 2016-05-12
    相关资源
    最近更新 更多