Вопрос

I'm trying to add AttributeRouting to my WebAPI project.

On one controller I currently have three GET methods defined:

[GET("dictionaries")]
public IEnumerable<Dictionary> Get()

[GET("dictionaries/{id}")]
public Dictionary GetByID(int id)

[GET("dictionaries/{dictionaryID}/{page}")]
public Dictionary Browse(long dictionaryID, int page)

The first two routes are working as I expect them to, but the third always returns a 405 METHOD NOT ALLOWED.

I've tried sending the parameters in the URL, and the query-string, and it's the same response for both. When I've tried the query-string version, I've modified the route to be

[GET("dictionaries?dictionaryID={dictionaryID}&page={page}

I've also tried changing the initial word from dictionaries to dictionary to avoid any ambiguity with the other two GET routes, but still no success.

The documentation for AttributeRouting only mentions query-strings in relation to parameter constraints (which aren't available for me because of the WebHost framework) and doesn't mention how query-strings can be used in routing.

Can anyone tell me how I can achieve this third route with those two parameters, or do I have to drop AttributeRouting or try on a controller of its own?

Это было полезно?

Решение

Web API action selector implicitly thinks that the third action here is a POST, since it doesn't start with verbs like GET, POST, PUT etc. Try adding HttpGet attribute also and see if this works.

[HttpGet, GET("dictionaries/{dictionaryID}/{page}")]  
public Dictionary Browse(long dictionaryID, int page)
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top