【问题标题】:Why does Post work, but not Get, with the same route and args?为什么 Post 可以使用相同的路由和 args 而不是 Get?
【发布时间】:2015-12-31 20:03:53
【问题描述】:

我有以下控制器代码:

namespace PlatypusReports.Controllers
{
    [RoutePrefix("api/platypus")]
    public class PlatypusController : ApiController
    {
        [Route("{unit}/{begindate}")]
        [HttpPost]
        public void Post(string unit, string begindate)
        {
            . . .
        }

        [Route("{unit}/{begindate}")]
        [HttpGet]
        public string Get(string unit, string begindate)
        {
            . . .
        }
        . . .         

调用 POST 方法有效,但调用 GET 方法无效;在后一种情况下,我得到“405 方法不允许 - 请求的资源不支持 http 方法 'GET'。”

我使用来自 Postman 的完全相同的 URL 给他们打电话:

http://localhost:52194/api/platypus/poisontoe/201509

...唯一的区别是我选择“POST”时它起作用,而当我选择“GET”时它不起作用。

为什么 POST 有效,但 GET 无效?我必须在我的 GET 代码中进行哪些更改才能获得支持/允许?

【问题讨论】:

  • 执行GET的时候,可以用fiddler抓取请求payload吗?我认为您可能缺少两个输入参数的查询字符串。

标签: c# post controller get asp.net-web-api-routing


【解决方案1】:

如果您使用的是 apicontroller,则不需要装饰器 HttpPost 和 HttpGet。如果您删除它们,它应该可以工作。

【讨论】:

  • 恰如雨下;事实上,你甚至比紫雨还对,这简直是太奇怪了。谢天谢地,紫色恐怖统治似乎已成为历史。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-04
  • 2015-02-12
  • 2016-12-23
相关资源
最近更新 更多