Reputation: 2524
I have a generic Web API controller that does CRUD which is like this:
public class BaseController : ApiController {
[HttpPost]
public virtual IHttpActionResult Post(T entity)
{
return Ok();
}
[HttpPut]
public virtual IHttpActionResult Put(T entity)
{
return Ok();
}
}
ObjectController is derived from BaseController but it overrides the Put Method
public class ObjectController : BaseController {
[HttpPut]
[Route("api/Object")]
public override IHttpActionResult Put(T entity)
{
return Ok();
}
}
I have no problem calling all methods in a controller when none is overridden. For example,
public class Object2Controller : BaseController { }
Invoking the POST method returns 405 method not allowed. What is wrong with my code?
EDIT This is not a duplicate question because I can call PUT in other controllers.
EDIT 2 Changed new to override(virtual) but still getting the same error
EDIT 3 Provided a working controller
Upvotes: 0
Views: 1422
Reputation: 3778
Why not use VIRTUAL keyword in your base class? .. VIRTUAL is made specifically to let developer ovverride methods in derived classes
something like:
public class BaseController : ApiController {
[HttpPost]
public virtual IHttpActionResult Post(T entity)
{
return Ok();
}
[HttpPut]
public virtual IHttpActionResult Put(T entity)
{
return Ok();
}
}
and then:
public class ObjectController : BaseController {
[HttpPut]
[Route("api/Object")]
public override IHttpActionResult Put(T entity)
{
return Ok();
}
}
Hope it helps you!
Upvotes: 1