Search code examples
c#asp.net-coreswaggerwebapi

WebAPI and OData8 conflict with Controller names in Swagger


I have an issue which seems common from what I've researched around my controller names conflicting and issues with swagger.

If I have two Get() functions in Controller one and two Get() functions in Controller two I receive the error:

Conflicting method/path combination "GET {id}" for actions

Important to note:

  • This error only appears if within my controller I directly call the ODataController like below:
public class OrdersController : ODataController
  • This error does not appear if I do
public class OrdersController : BaseApiController
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.OData.Routing.Controllers;

namespace API.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class BaseApiController : ODataController
    {
        
    }
}

The main problem I have now is Swagger displaying two sets of endpoints:

one for all my /api/<controller> endpoints as well as /odata/<controller> (duplicated) and I only want the odata ones, but if I remove the Route the error will reoccur.


Solution

  • one for all my /api/ endpoints as well as /odata/ (duplicated) and I only want the odata ones. But if I remove the Route the error will reoccur

    Remove both[ApiController] and [Route("api/[controller]")]

    Here's an example of OData Controller in this document