我在我的客户端js脚本中使用这样的fetch发布数据
fetch('/myarea/mycontroller/myaction', {
method: 'post',
body: JSON.stringify({ name: namevalue, address: addressvalue })
})
.then(function (response) {
if (response.status !== 200) {
console.log('fetch returned not ok' + response.status);
}
response.json().then(function (data) {
console.log('fetch returned ok');
console.log(data);
});
})
.catch(function (err) {
console.log(`error: ${err}`);
});
}, false);
Run Code Online (Sandbox Code Playgroud)
然后在我的控制器上
[HttpPost]
public async Task<IActionResult> MyAction(string name, string address)
{
// Error, name and address is null here, shouldn't be!
// more code here ...
}
Run Code Online (Sandbox Code Playgroud)
正在调用我的控制器操作,我可以在其中调试,但没有传递数据.这可能有什么问题?谢谢
我想使用属性来确保我的请求中存在某些标头.这不是授权属性.其中一个用例是收到请求时,我想确保老客户端有X-Request-For标头,可以正确处理.还有其他用例,但是所有这些用例都会在控制器收取费用之前读取特定的http标头值并采取适当的操作.
[MyAttribute(HeaderOptions.RequestFor)
[httpPost]
public MyMethod(string data)
{
...
}
Run Code Online (Sandbox Code Playgroud)