This error is very common, and I tried all of the solutions and non of them worked. I have disabled WebDAV publishing in control panel and added this to my web config file:
<handlers> <remove name="WebDAV"/> </handlers> <modules runAllManagedModulesForAllRequests="true"> <remove name="WebDAVModule"/> </modules>
The error still persists. This is the controller:
static readonly IProductRepository repository = new ProductRepository(); public Product Put(Product p) { return repository.Add(p); }
Method implementation:
public Product Add(Product item) { if (item == null) { throw new ArgumentNullException("item"); } item.Id = _nextId++; products.Add(item); return item; }
And this is where the exception is thrown:
client.BaseAddress = new Uri("http://localhost:5106/"); client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json")); var response = await client.PostAsJsonAsync("api/products", product);//405 exception
Any suggestions?
The HyperText Transfer Protocol (HTTP) 405 Method Not Allowed response status code indicates that the server knows the request method, but the target resource doesn't support this method. The server must generate an Allow header field in a 405 status code response.
If you are certain you need a POST request, chances are, you're just using the wrong endpoint on the server. Again, this can only be solved if you have proper documentation to show what methods are supported for each endpoint.
You are POSTing from the client:
await client.PostAsJsonAsync("api/products", product);
not PUTing.
Your Web API method accepts only PUT requests.
So:
await client.PutAsJsonAsync("api/products", product);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With