WebApi HttpPost不使用参数

时间:2016-06-15 13:15:27

标签: asp.net-web-api asp.net-web-api2

我有一个带有此路由配置的Web Api项目

    var config = GlobalConfiguration.Configuration;
    config.Routes.MapHttpRoute(
        "DefaultApiRoute",
        "api/v1/{controller}/{action}");

我有两个帖子方法

[HttpPost]
public IHttpActionResult RetrieveTestNoParam()
{
    return new JsonResult<string>("This is a test", new JsonSerializerSettings(), Encoding.UTF8, this);
}


[HttpPost]
public IHttpActionResult RetrieveTest(string input)
{
    return new JsonResult<string>(input, new JsonSerializerSettings(), Encoding.UTF8, this);
}

第一个是工作,但第二个给出了这个响应

{
  "Message": "No HTTP resource was found that matches the request URI 'http://test.dev.local/api/v1/NewsApi/RetrieveTest'.",
  "MessageDetail": "No action was found on the controller 'NewsApiController' that matches the request."
}

这就是我在fiddler中看到的

POST http://test.dev.local/api/v1/NewsApi/RetrieveTest HTTP/1.1
Host: test.dev.local
Connection: keep-alive
Content-Length: 261
Cache-Control: no-cache
Origin: chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop
Content-Type: application/javascript
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36
Postman-Token: c2ba631b-4bfd-9379-bf40-0ca1a6a8a3bc
Accept: */*
DNT: 1
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8,nl;q=0.6
Cookie: ASP.NET_SessionId=4kgfwd2u3pwusfdvdsrkt2vt; SC_ANALYTICS_GLOBAL_COOKIE=ddd9185a3e8940c9823e81b7d38cdf20|False; sc_expview=0

------WebKitFormBoundaryxdYhzJ7A8WnI8qaa
Content-Disposition: form-data; name="input"

Dit is de input
------WebKitFormBoundaryxdYhzJ7A8WnI8qaa
Content-Disposition: form-data; name="czczcsdcsdf"

sdfsfsdfsdff
------WebKitFormBoundaryxdYhzJ7A8WnI8qaa--

有什么想法吗?

我对Web Api的其他问题:

控制器可以包含多个get或post方法吗? 我是否需要actionname属性以及此属性的作用?

此致 丹尼

0 个答案:

没有答案