简体   繁体   English

Web API 参数始终为空

[英]Web API parameter is always null

I have the following method in my Web API:我的 Web API 中有以下方法:

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

The FileUserModel is defined as: FileUserModel定义为:

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

I'm trying to call this through Fiddler but whenever I do the model is always set to null.我试图通过 Fiddler 调用它,但是每当我这样做时,模型总是设置为 null。 In Fiddler I've sent the composer to use POST , the url is there and correct as the debugger in Visual Studio breaks when called.在 Fiddler 中,我已发送 Composer 使用POST ,该 url 在那里并且正确,因为 Visual Studio 中的调试器在调用时中断。 The Request I've setup as:我设置为的请求:

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

The Request Body as:请求正文为:

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

But whenever I run the composer when the debugger hits the breakpoint it always shows model is null.但是每当我在调试器遇到断点时运行 Composer 时,它总是显示模型为空。

You're missing a , in the request that you're sending.您在发送的请求中缺少, Also, because of the enclosing double quotes, you're actually sending a JSON string, not a JSON object.此外,由于包含双引号,您实际上发送的是 JSON 字符串,而不是 JSON 对象。 Removing the quotes and adding the comma should fix your problem.删除引号并添加逗号应该可以解决您的问题。

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

Also, since you're posting a model, you don't need the [FromBody] attribute.此外,由于您要发布模型,因此不需要[FromBody]属性。

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

That should be just fine.那应该没问题。 For more information on this, see this blog .有关这方面的更多信息,请参阅此博客

You need to make ajax call like below你需要像下面这样进行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"
    });
});

Dont forget to mark content type as json and on server side api code不要忘记将内容类型标记为 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 在 ASP.NET Web API 中发送 HTML 表单数据

The problem you have is you are posting the JSON data with surrounding double quotes.您遇到的问题是您正在发布带有双引号的 JSON 数据。 Remove them and it should work.删除它们,它应该可以工作。

Also fix the missing comma:还要修复丢失的逗号:

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM