使用GET的MVC AttributeRouting - 返回405 - 不允许的方法

时间:2013-06-21 12:58:22

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

我刚刚开始研究一种新的控制器动作方法,我有点困惑,为什么我会看到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网址,我可以在表格中看到如下条目:

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'"

完全不知道这件事。

我还能尝试哪些方法来解决出错的问题?

1 个答案:

答案 0 :(得分:5)

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

405 when using AttributeRouting.PUTAttribute unless I also include HttpPutAttribute