Search code examples
.net-coreasp.net-web-api.net-6.0ambiguous

How to Overload HttpPost Web API Method Based Json Datatype Properties


I am asked to implement a REST Web API to a specific route, where either of two different Json Datatypes may be posted.

This results in the following exception being thrown:

Microsoft.AspNetCore.Routing.Matching.AmbiguousMatchException: The request matched multiple endpoints.

Is there an Attribute that can be placed on the Web Methods, referencing Properties of the Json payloads so as to disambiguate the two possible Datatypes?


Solution

  • This was covered here but I'll add a little bit.

    It's not good API design to do that and goes against Swagger / OpenAPI specifications to do what you're asking.

    The only way to do this with the same HTTP method (POST in your case) is to have one action that takes in both models. Check which one isn't null to then route to the correct method to handle that logic.

    If you can get away with using a different HTTP verb you could technically do that and have two separate action methods (like POST and PUT), but you wouldn't be using them "correctly" and based on your question and need, I doubt you can do that anyway.