Resolve "The Server Responded with a Status of 405 (Method Not Allowed)" Error In Web API

I was working with Web API for one of my new projects, and suddenly I got this error “the server responded with a status of 405 (method not allowed)” when I tried to delete a record through API. I started to scratch my head. And finally I got the solution for the same. Here I am going to share with you that solution. I hope you will like this.

Following are the solutions that you can try out if you get this error.

Solution 1: Changing Web.Config file.

This is the first thing you must try. Please add the below tags in the Web.Config file under.

  1. validation validateIntegratedModeConfiguration="false"/>  
  2. <modules runAllManagedModulesForAllRequests="true">  
  3.    <remove name="WebDAVModule"/>  
  4. </modules>  
If you are getting the error still, try adding one tag under handlers as follows.

<handlers>
   <remove name="WebDAV" />
   .....
</handlers>


If the above didn’t work for you, you can go to the next solution.

Solution 2: Revert back the parameter name

As you all know we have a file called WebApiConfig.cs where we set the MapHttpRoute and other config filters.

WebAPI

By default the parameter here is ‘id’ as follows.
  1. config.Routes.MapHttpRoute(  
  2.    name: "DefaultApi",  
  3.    routeTemplate: "api/{controller}/{id}",  
  4.    defaults: new { id = RouteParameter.Optional }  
  5. );  
Now if you changed the parameter of your function in any case, you need to revert it back to ‘id’, or you need to make changes in the WebApiConfig.cs file.

For Example, below is my delete function.
  1. // DELETE: api/Subscriber/5  
  2. public void Delete(int subId)  
  3. {  
  4.     tbl_Subscribers dlt = myEntity.tbl_Subscribers.Find(subId);  
  5.     if (dlt != null)  
  6.     {  
  7.         try  
  8.         {  
  9.             myEntity.tbl_Subscribers.Remove(dlt);  
  10.             myEntity.SaveChanges();  
  11.         }  
  12.         catch (Exception)  
  13.         {  
  14.             throw;  
  15.         }  
  16.     }  
  17. }  
As you can see I have changed my parameter to ‘subId.’ Because of this I was getting the error “the server responded with a status of 405 (method not allowed)” always. Then I changed my function as follows.
  1. // DELETE: api/Subscriber/5  
  2. public void Delete(int id)  
  3. {  
  4.     tbl_Subscribers dlt = myEntity.tbl_Subscribers.Find(id);  
  5.     if (dlt != null)  
  6.     {  
  7.         try  
  8.         {  
  9.             myEntity.tbl_Subscribers.Remove(dlt);  
  10.             myEntity.SaveChanges();  
  11.         }  
  12.         catch (Exception)  
  13.         {  
  14.             throw;  
  15.         }  
  16.     }  
  17. }  
It works fine after I have changed the parameter name. I hope you find it helpful. That is all. We did it. Happy coding.

Conclusion

Did I miss anything that you may think is needed? Did you try Web API yet? Have you ever wanted to do this requirement? Did you find this post useful? I hope you liked this article. Please sharewith me your valuable suggestions and feedback.

Your turn. What do you think?

A blog isn’t a blog without comments, but do try to stay on topic. If you have a question unrelated to this post, you’re better off posting it on C# Corner, Code Project, Stack Overflow, ASP.NET Forum instead of commenting here. Tweet or email me a link to your question there and I’ll definitely try to help if I can.

Read this article in my blog here.

 

Ebook Download
View all
Learn
View all