Home » Jquery » jquery – Why web api gives 'the requested resource does not support httpmethod get ' while using only POST method?

jquery – Why web api gives 'the requested resource does not support httpmethod get ' while using only POST method?

Posted by: admin February 22, 2020 Leave a comment

Questions:

I encountered a weird situation while using web api in an mvc web application. I want to use Post method of one api controller from a cshtml view. On this view I have this call (using jquery):

  function handOver(cardnumber) {
        var CreateHandOverTakeOvers = new Object();
        CreateHandOverTakeOvers.CardId = cardnumber;
        CreateHandOverTakeOvers.DateId = getFulldatestring();
        CreateHandOverTakeOvers.ShiftT = getShiftTexts();
        $.ajax({
            url: getRootUrl() + 'api/handovertakeoverapi/HandOverShiftForUser',
            type: 'POST',
            dataType: 'json',
            data: CreateHandOverTakeOvers,
            success: function (data, textStatus, xhr) {
                alert('success');
                //... some other api calls... calling GET on another action with different name ( 
                //  GetHandOverList to be precise) Could this interfere ?
            },
            error: function (xhr, textStatus, errorThrown) {
                alert(JSON.parse(xhr.responseText).Message;

            }
        });

    }

On server side this hits this web api controller:

 [Route("api/handovertakeoverapi/{action}", Name = "handovertakeoverapi")]
 public class HandOverTakeOverApiController : ApiController
 {
   ...

 [HttpPost]
        public IHttpActionResult HandOverShiftForUser([FromBody]CreateHandOverTakeOvers createHandOverTakeOvers)
        {

           // doing some database operations -using this for create row in mssql
           // giving back matching object string type field representing a string column of the created 
           // row 

            return Ok(h.HandOverLogin);
        }


   ...
 }

Hosted on IIS that ajax call returns Http error 500 – Internal server error after looking in Google Chrome’s developer console, BUT(!) the database operations complete everytime successfully. So I guess there is no error in this section.

Javascript alert : An error has occured.

The error it gives : ‘the requested resource does not support httpmethod get’ . I do not know why this keeps happening. There is no ‘Get’ type request on this action in the whole application. This does what it has to do, but gives back the error mentioned. (Although it is working perfectly on localhost, which is again a mistery…)

Any idea what is going on here? I am really clueless.

How to&Answer: