Web API 参数始终为空
Web API parameter is always null
我的网站有以下方法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"
}"
但是每当我 运行 composer 当调试器到达断点时它总是显示模型为空。
您发送的请求中缺少 ,
。此外,由于包含双引号,您实际上发送的是 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。
您需要像下面那样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;
}
您遇到的问题是您发布的 JSON 数据带有双引号。删除它们,它应该可以工作。
同时修复缺失的逗号:
{
"domain": "dcas",
"username": "sample string 2",
"password": "sample string 3",
"fileName": "sample string 4"
}
我的网站有以下方法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"
}"
但是每当我 运行 composer 当调试器到达断点时它总是显示模型为空。
您发送的请求中缺少 ,
。此外,由于包含双引号,您实际上发送的是 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。
您需要像下面那样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;
}
您遇到的问题是您发布的 JSON 数据带有双引号。删除它们,它应该可以工作。
同时修复缺失的逗号:
{
"domain": "dcas",
"username": "sample string 2",
"password": "sample string 3",
"fileName": "sample string 4"
}