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:
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();
}
}