0

In my ASP.NET web application I have two types of controllers:

1) Web API controllers: 'System.Web.Http.ApiController'

2) Odata controllers: 'Microsoft.AspNet.OData.ODataController'

I have a problem with PUT and DELETE methods- I'm creating these methods only in ApiController but when I run the application I see these endpoints also as Odata endpoints. The delete method is decorated by [HttpDelete] attribute:

[HttpDelete]
[Route("{id:int}")]
public IHttpActionResult RemoveEmployee(int id)
{
    return Ok();
}

In the OData controller class, I'm creating only the Get method.

In Swagger it looks like this - DELETE endpoint is duplicated:

Swagger

ODataController class:

    [ApiVersion( "2.0" )]
    [ODataRoutePrefix( "Employee" )]
    public class EmployeeController : ODataController
    {
        [HttpGet]
        [ODataRoute]
        [ResponseType( typeof( ODataValue<IEnumerable<Employee>> ) )]
        [EnableQuery( MaxTop = 100, AllowedQueryOptions = Select | Top | Skip | Count )]
        public IQueryable<Employee> Get()
        {
            var empl = new[]
{
                new Employee(){ EmplId = 11, Name = "John Smith", Age = 50 },
                new Employee(){ EmplId = 21, Name = "Bob Smith", Age = 70}
                        };

            return empl.AsQueryable();
        }
    }

ApiController class:

    [ApiVersion( "2.0" )]
    [RoutePrefix( "api/Employee" )]
    public class EmployeeController : ApiController
    {

        [HttpGet]
        [Route( "" )]
        public string Get()
        {
            var empl = new[]
{
                new Employee(){ EmplId = 11, Name = "John Smith", Age = 50 },
                new Employee(){ EmplId = 21, Name = "Bob Smith", Age = 70}
                        };

            string ret = string.Empty;

            foreach ( var item in empl )
            {
                ret += $"{item.EmplId}, {item.Name}, {item.Age.ToString()}";
            }

            return ret;
        }

        [HttpPost]
        [Route("")]
        public IHttpActionResult InsertEmployee(Employee employee)
        {
            return Ok();
        }

        [HttpDelete]
        [Route("{id:int}")]
        public IHttpActionResult RemoveEmployee([FromUri] int id)
        {
            return Ok();
        }

    }
3
  • can you add the code in your controllers? Commented Mar 3, 2022 at 7:28
  • try renaming one of your controllers so they do not have the same name Commented Mar 3, 2022 at 19:49
  • @Rafal If I rename e.g. api controller I have endpoints related to this controller in separate group. I would like to have in the same group e.g. 'Employee'. Commented Mar 4, 2022 at 8:08

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.