我在服务器端(c#, core 3.1):
[EnableCors("CorsApi")]
[Route("[action]", Name = "DeleteFile")]
[HttpPost]
public List<string> DeleteFile(string pathFile)
{
string commandDelete = "rm " + pathFile;
List<string> results = ExecuteCommand(commandDelete, true);
return results;
}
,我有这个在客户端(反应):
(async () => {
const response = await fetch('https://localhost:7198/api/Proj/DeleteFile', {
method: 'POST',
body: JSON.stringify({
pathFile: srcFile,
}),
headers: {
'Content-type': 'application/json; charset=UTF-8'
},
})
const data = await response.json()
console.log(data)
})()
但是POST不起作用。只有当我在url中发送参数时,它才能工作,比如:
'https://localhost:7198/api/Proj/DeleteFile?pathFile=' + srcFile.
为什么会这样?我该怎么补救呢?
谢谢。
您是否可以尝试像这样在c#中为参数添加[FromBody]属性
public List<string> DeleteFile([FromBody] string pathFile)
这应该绑定pathFile