0

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

rajeemcariazo
  • 2,476
  • 5
  • 36
  • 62
  • 4
    That isn't overriden, you've simply hidden the original method. BaseController should declare `public virtual IHttpActionResult Put(T entity)` and ObjectController should declare `public override IHttpActionResult Put(T entity)`. See [here](https://stackoverflow.com/questions/1399127/difference-between-new-and-override) for new vs override. – ProgrammingLlama Jul 19 '18 at 07:56
  • 2
    what is the Uri of your request – Code Name Jack Jul 19 '18 at 07:57
  • @rajeemcariazo if you *can* use PUT in other controllers there's no reason to chase WebDAV. You have to make the base `PUT` virtual though otherwise and use the `override` keyword because right now you aren't overriding anything. – Panagiotis Kanavos Jul 19 '18 at 08:07
  • @rajeemcariazo you can't just say "I can use PUT in other controllers" though. You have to provide a *reproducible* example, *and* provide your `web.config` so people don't have to guess about WebDAV. What URL causes the problem? Does it differ in any way compared to other PUTs ? If you don't provide the actual configuration, URLs, code, people would have to guess just to try to reproduce the problem – Panagiotis Kanavos Jul 19 '18 at 08:09
  • @rajeemcariazo without that information, I'm afraid this question will have to be closed. Besides, I suspect that simply trying to create a new, simple Web API project that exhibits the same problem will either not reproduce it or show you what's wrong. A missed web.config setting? Or a WebDAV setting on *one* Application pool/Virtual Directory? Do you use *Areas* perhaps with their own web.config? Tried to host under a SharePoint root? – Panagiotis Kanavos Jul 19 '18 at 08:12

1 Answers1

1

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!

federico scamuzzi
  • 3,708
  • 1
  • 17
  • 24