Web API POST not found (throws 404 on browser) but works on PostMan and Swagger

My Web API returns 404 when called from AJAX but works well on PostMan and Swagger.

Post method

// POST api/<controller>
[HttpPost]
public int Post(string url, string RData, string aid, string AuthToken)
{
       //do something
}

jQuery AJAX call:

 function SaveData() {
            var $form = $("#formMain");
            var data = Serialize($form);
            var RData = JSON.stringify(data);
            var Url = window.location.href;
            var aid = "12332";
            //Get Auth Token

            $.ajax({
                url: "/api/Register/MyAPIKey", //Getting Auth Token first
                type: "GET",
                success: function (res) {
                    let AuthToken = res;
                    //After receiving Auth token, finally submit the registration data. This throws 404 error only on browser.
                    $.ajax({
                        type: "POST",
                        url: '/api/Register',
                        data: {
                            'url': Url,
                            'RData': RData,
                            'aid': aid,
                            'AuthToken': AuthToken
                        },
                        success: function (data) {
                            //Show success message
                        }
                    });
                }
            });
        }

The error I get in browser console is:

Message: "No HTTP resource was found that matches the request URI 'http://localhost:54318/api/Register'."
MessageDetail: "No action was found on the controller 'Register' that matches the request."

Postman Screenshot: Please note -1 is expected in response, since some other validation in the API method is incorrect. But we can ignore that for now, since the status is 200.

Web API POST not found (throws 404 on browser) but works on PostMan and Swagger

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The issue was resolved when I replaced the individual parameters in the Post method with an Object having all these as properties.

[HttpPost]
public int Post(PostData Reg)
{
   //do something
}

public class PostData
{
    public string url { get; set; } 
    public string RData { get; set; }
    public string aid { get; set; }
    public string AuthToken { get; set; }
}

And finally setup the dataType in the ajax method as JSON.

dataType: "json"


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x