小编eri*_*zic的帖子

如何使用asp.net核心中的Fetch api将数据传递给控制器

我在我的客户端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)

正在调用我的控制器操作,我可以在其中调试,但没有传递数据.这可能有什么问题?谢谢

c# asp.net-core-mvc asp.net-core

13
推荐指数
2
解决办法
9125
查看次数

在ASP.net核心中创建自定义验证属性

我想使用属性来确保我的请求中存在某些标头.这不是授权属性.其中一个用例是收到请求时,我想确保老客户端有X-Request-For标头,可以正确处理.还有其他用例,但是所有这些用例都会在控制器收取费用之前读取特定的http标头值并采取适当的操作.

[MyAttribute(HeaderOptions.RequestFor)
[httpPost]
public MyMethod(string data) 
{
...
}
Run Code Online (Sandbox Code Playgroud)

c# asp.net-core

1
推荐指数
1
解决办法
5547
查看次数

标签 统计

asp.net-core ×2

c# ×2

asp.net-core-mvc ×1