MVC AttributeRouting With GET - Returning 405 - Method Not Allowed

梦想的初衷 提交于 2019-12-01 06:40:31

问题


I've just been working on a new controller action method and I'm a little confused why I'm seeing a 405.

I have defined several GET attributed methods on my API already and they all function as expected. For an example this works fine:

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

Yet my new method defined like this:

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

Is returning a 405

If I visit the routes.axd url on the API I can see an entry in the table like this:

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

This all looks correct. On the client side I'm using the same approach on both requests using an HttpClient:

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

Running a get from Fiddler also returns a 405.

Even looking at the RequestMessage in the response looks correct:

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

Totally stumped on this.

What else can I try to maybe shed some light on what is going wrong?


回答1:


You would need to decorate the Search action with one more attribute called System.Web.Http.HttpGetAttribute. For the reason behind this, you can take a look at my answer in the below post:

405 when using AttributeRouting.PUTAttribute unless I also include HttpPutAttribute



来源:https://stackoverflow.com/questions/17235826/mvc-attributerouting-with-get-returning-405-method-not-allowed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!