Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

405 method not allowed Web API

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?

like image 409
Xardas Avatar asked Mar 30 '13 12:03

Xardas


People also ask

What does 405 Method Not Allowed mean?

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.

How do I fix 405 Method not allowed in Postman?

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.


1 Answers

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); 
like image 130
Darin Dimitrov Avatar answered Oct 27 '22 01:10

Darin Dimitrov