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

 

 

 

 

Top