C# 如何将类型为的对象作为参数传递给Web Api Get/Post方法
这是我的Post方法,我正在传递一个包含参数的对象C# 如何将类型为的对象作为参数传递给Web Api Get/Post方法,c#,asp.net-mvc,api,asp.net-web-api,routes,C#,Asp.net Mvc,Api,Asp.net Web Api,Routes,这是我的Post方法,我正在传递一个包含参数的对象 public class TransactionController : ApiController { [HttpPost] public TransactionResponse mytest2(TransactionOperationInput input) { // some operation here } } 这是我要测试的URL http://localhost:33755/api/Tra
public class TransactionController : ApiController
{
[HttpPost]
public TransactionResponse mytest2(TransactionOperationInput input)
{
// some operation here
}
}
这是我要测试的URL
http://localhost:33755/api/Transaction/mytest2?SourceKey=abcdef&Pin=123&Criteria=2018-09-12 00:00:00
方法设置为POST和Get时的结果
[HttpPost]:我得到一个错误405方法不允许
[HttpGet]:输入参数为空
这是注册路由类;(对不起,我不熟悉路由表)
我需要更新路由表吗?还是我的网址错了
另外,我应该将服务方法设置为Post还是Get?您无法导航到
[HttpPost]
方法,因此它必须是[HttpGet]
。要从查询字符串值绑定到复杂对象,需要使用[FromUri]
属性
[HttpGet] // can be omitted
public TransactionResponse mytest2([FromUri]TransactionOperationInput input)
有关web api中参数绑定的更多信息,请参阅。您无法导航到POST方法(它需要是
[HttpGet]
)
[HttpGet] // can be omitted
public TransactionResponse mytest2([FromUri]TransactionOperationInput input)