3

When building a RESTful API, it is desirable to issue return codes of 406 (not acceptable) and 415 (Unsupported media type) when doing content negotiation.

What is not clear to me is exactly how to return these codes in a WebAPI2 app that is using attribute routing.

I have seen examples on how to use ActionMethodSelectorAttribute and RouteFactoryAttribute to create custom route selectors, but when a route cannot be found due to an Accept or Content-Type mismatch, the automatic response is a 404 Not found error, which does not fully explain the problem.

What I would like to do is capture WHY a route could not be found, and if it is due to a content type negotiation failure, return the appropriate 406 or 415 response code.

Is there an example anywhere that shows how to do this?

mcwrath
  • 45
  • 4
  • 1
    Ok, my question my not be clear... What I am attempting to do is implement route selection based on version provided in the accept type in the request header, as described here: http://stackoverflow.com/questions/25299889/customize-maphttpattributeroutes-for-web-api-versioning The problem I have is that when the route is not selected because of a version mismatch, I would like the ability to return a 406. – mcwrath Apr 19 '16 at 14:31
  • Did you found a solution for this? – TrustworthySystems Jun 11 '22 at 07:26

0 Answers0