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

Jam*_*mer 3 asp.net-mvc asp.net-web-api attributerouting

我刚刚开始研究一种新的控制器动作方法,我有点困惑,为什么我看到405.

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

    [GET("entries/{page}"), JsonExceptionFilter]
    public HttpResponseMessage GetEntries(int page)
Run Code Online (Sandbox Code Playgroud)

然而我的新方法定义如下:

    [GET("search/{searchTerm}/{page}"), JsonExceptionFilter]
    public HttpResponseMessage Search(string searchTerm, int page)
Run Code Online (Sandbox Code Playgroud)

回来了405

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

GET, HEAD, OPTIONS  users/search/{searchTerm}/{page}
Run Code Online (Sandbox Code Playgroud)

这看起来都很正确.在客户端,我在使用HttpClient的两个请求上使用相同的方法:

var response = httpClient.GetAsync(ApiRootUrl + "users/search/" + searchTerm + "/" + page).Result;
Run Code Online (Sandbox Code Playgroud)

从Fiddler那里得到一个得分也会返回405.

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

"{Method: GET, RequestUri: 'http://localhost:51258/users/search/jam/0'"
Run Code Online (Sandbox Code Playgroud)

完全被这个困扰了.

我还能尝试什么来解释出现什么问题?

Kir*_*lla 5

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

405使用AttributeRouting.PUTAttribute时除非我还包含HttpPutAttribute