问题
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