Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

DZone's Guide to

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
Free Resource

Add user login and MFA to your next project in minutes. Create a free Okta developer account, drop in one of our SDKs to your application and get back to building.

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.

Launch your application faster with Okta’s user management API. Register today for the free forever developer edition!

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.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}