ASP.NET Core API 中的模型绑定始终为 null
Model binding in ASP.NET Core API are always null
我有一个简单的 ASP.NET 核心 API,我用 javascript 的获取方法调用它。但是 SearchString 属性 类型的输入变量 model 始终为 null。
当我使用 Postman 调用它时,它按预期工作,但出于某种原因,从 javascript 调用它时没有。
API:
[HttpPost]
public async Task<IActionResult> Search([FromBody] BasicSearchModel model)
{
....
}
BasicSearchModel.cs:
public interface IBasicSearchModel
{
string SearchString { get; set; }
}
public class BasicSearchModel : IBasicSearchModel
{
public string SearchString { get; set; }
}
JavaScript:
const WebRequest = async (controller = "API", action, query = {}, data = {}, method = RequestMethods.POST, mode = RequestModes.SameOrigin, creds = RequestCredentials.SameOrigin, headers = {
"Content-Type": "application/json",
"Accept": "application/json"
}) => {
const GetUrl = () => {
var result = "/" + controller + "/" + action;
var i = 0;
for (var q in query) {
if (i === 0) {
result += "?";
} else {
result += "&";
}
result += q[0] + "=" + q[1];
i++;
}
return result;
}
await fetch(GetUrl(), {
method: method,
headers: headers,
// @ts-ignore
credentials: creds.toLowerCase(),
// @ts-ignore
body: data,
// @ts-ignore
mode: mode.toLowerCase(),
})
.then(response => {
if (response.ok) {
return new RequestResult(response);
} else {
throw new Error("Request failed: " + response.status + "; " + response.statusText);
}
})
.catch(error => {
throw new Error("Error: " + error.statusText);
});
};
JavaScript 调用:
var result = await WebRequest("Identity", "Search", null, { SearchString: str });
请求:
Headers:
接受:"application/json"、"Content-Type":"application/json"
方法:
"POST"
Body:
搜索字符串:"tester"
当使用 Postman 调用时,它按预期工作,很可能是因为它的格式正确。
SearchString: "tester"
无效JSON
Stringyfy要发送的数据
await fetch(GetUrl(), {
method: method,
headers: headers,
// @ts-ignore
credentials: creds.toLowerCase(),
// @ts-ignore
body: JSON.stringify(data), //<--NOTE
// @ts-ignore
mode: mode.toLowerCase(),
})
以便模型绑定器能够解析正确的格式。
我有一个简单的 ASP.NET 核心 API,我用 javascript 的获取方法调用它。但是 SearchString 属性 类型的输入变量 model 始终为 null。
当我使用 Postman 调用它时,它按预期工作,但出于某种原因,从 javascript 调用它时没有。
API:
[HttpPost]
public async Task<IActionResult> Search([FromBody] BasicSearchModel model)
{
....
}
BasicSearchModel.cs:
public interface IBasicSearchModel
{
string SearchString { get; set; }
}
public class BasicSearchModel : IBasicSearchModel
{
public string SearchString { get; set; }
}
JavaScript:
const WebRequest = async (controller = "API", action, query = {}, data = {}, method = RequestMethods.POST, mode = RequestModes.SameOrigin, creds = RequestCredentials.SameOrigin, headers = {
"Content-Type": "application/json",
"Accept": "application/json"
}) => {
const GetUrl = () => {
var result = "/" + controller + "/" + action;
var i = 0;
for (var q in query) {
if (i === 0) {
result += "?";
} else {
result += "&";
}
result += q[0] + "=" + q[1];
i++;
}
return result;
}
await fetch(GetUrl(), {
method: method,
headers: headers,
// @ts-ignore
credentials: creds.toLowerCase(),
// @ts-ignore
body: data,
// @ts-ignore
mode: mode.toLowerCase(),
})
.then(response => {
if (response.ok) {
return new RequestResult(response);
} else {
throw new Error("Request failed: " + response.status + "; " + response.statusText);
}
})
.catch(error => {
throw new Error("Error: " + error.statusText);
});
};
JavaScript 调用:
var result = await WebRequest("Identity", "Search", null, { SearchString: str });
请求:
Headers: 接受:"application/json"、"Content-Type":"application/json"
方法: "POST"
Body: 搜索字符串:"tester"
当使用 Postman 调用时,它按预期工作,很可能是因为它的格式正确。
SearchString: "tester"
无效JSON
Stringyfy要发送的数据
await fetch(GetUrl(), {
method: method,
headers: headers,
// @ts-ignore
credentials: creds.toLowerCase(),
// @ts-ignore
body: JSON.stringify(data), //<--NOTE
// @ts-ignore
mode: mode.toLowerCase(),
})
以便模型绑定器能够解析正确的格式。