Over a million developers have joined DZone.

How to Solve "The Server Responded With a Status of 405 (Method Not Allowed)"

I was working with Web API for one of my new projects, when suddenly I got this error message: "the server responded with a status of 405 (method not allowed)" when I tried to delete a record through API. I began to scratch my head, and finally, I came up with a solution.

· Web Dev Zone

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.

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

Following are the various 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.

<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.

Image title

WebApi Config File

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.


 // DELETE: api/Subscriber/5
        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.


// DELETE: api/Subscriber/5
        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 which is needed? Did you try Web API yet? Have you ever wanted to do this requirement? Could you find this post as useful? I hope you liked this article. Please share with me your valuable suggestions and feedback.

Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.

Topics:
web api ,.net ,api ,rest

Published at DZone with permission of Sibeesh Venu, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}