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.
- validation validateIntegratedModeConfiguration="false"/>
- <modules runAllManagedModulesForAllRequests="true">
- <remove name="WebDAVModule"/>
- </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.
By default the parameter here is ‘id’ as follows.
- config.Routes.MapHttpRoute(
- name: "DefaultApi",
- routeTemplate: "api/{controller}/{id}",
- defaults: new { id = RouteParameter.Optional }
- );
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.
-
- public void Delete(int subId)
- {
- tbl_Subscribers dlt = myEntity.tbl_Subscribers.Find(subId);
- if (dlt != null)
- {
- try
- {
- myEntity.tbl_Subscribers.Remove(dlt);
- myEntity.SaveChanges();
- }
- catch (Exception)
- {
- throw;
- }
- }
- }
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.
-
- public void Delete(int id)
- {
- tbl_Subscribers dlt = myEntity.tbl_Subscribers.Find(id);
- if (dlt != null)
- {
- try
- {
- myEntity.tbl_Subscribers.Remove(dlt);
- myEntity.SaveChanges();
- }
- catch (Exception)
- {
- throw;
- }
- }
- }
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.