【问题标题】:MVC AttributeRouting With GET - Returning 405 - Method Not Allowed带有 GET 的 MVC AttributeRouting - 返回 405 - 方法不允许
【发布时间】:2013-06-21 12:58:21
【问题描述】:

我一直在研究一种新的控制器操作方法,但我有点困惑为什么会看到 405。

我已经在我的 API 上定义了几个 GET 属性方法,它们都按预期运行。例如,这很好用:

    [GET("entries/{page}"), JsonExceptionFilter]
    public HttpResponseMessage GetEntries(int page)

然而我的新方法是这样定义的:

    [GET("search/{searchTerm}/{page}"), JsonExceptionFilter]
    public HttpResponseMessage Search(string searchTerm, int page)

返回 405

如果我访问 API 上的 routes.axd url,我可以在表格中看到如下条目:

GET, HEAD, OPTIONS  users/search/{searchTerm}/{page}

这一切看起来都是正确的。在客户端,我使用 HttpClient 对两个请求使用相同的方法:

var response = httpClient.GetAsync(ApiRootUrl + "users/search/" + searchTerm + "/" + page).Result;

从 Fiddler 运行 get 也会返回 405。

即使查看响应中的 RequestMessage 看起来也是正确的:

"{Method: GET, RequestUri: 'http://localhost:51258/users/search/jam/0'"

完全被这件事难住了。

我还可以尝试什么来阐明问题所在?

【问题讨论】:

    标签: asp.net-mvc asp.net-web-api attributerouting


    【解决方案1】:

    您需要使用另一个名为System.Web.Http.HttpGetAttribute 的属性来装饰搜索操作。对于这背后的原因,您可以在下面的帖子中查看我的答案:

    405 when using AttributeRouting.PUTAttribute unless I also include HttpPutAttribute

    【讨论】:

    • 拍前额...干得好,我什至知道命名约定规则,看不到树木的树木。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2017-11-13
    • 1970-01-01
    • 1970-01-01
    • 2020-08-25
    • 2012-04-21
    • 2016-01-31
    • 2013-03-23
    • 1970-01-01
    相关资源
    最近更新 更多