[Solved] ASP.Net Web API Ajax call Error: No HTTP resource was found that matches the request URI

nauna
 
on Sep 10, 2021 11:00 PM
Sample_208893.zip
665 Views

hello,

this is the web api post method which take single parameter

[HttpPost]
public HttpResponseMessage PostStudent(string studentname)
{ 
    HttpResponseMessage response = null;
    if (ModelState.IsValid)
    {
        response = Request.CreateResponse(HttpStatusCode.OK, "Posted Successfully");
    }
 
    return response;
}

when i call it like this to post textbox value in data field so it gives error

it says 404 not found 

POST http://localhost:61057/api/JqueryAjax/PostStudent 404 (Not Found)

  1. Message: "No HTTP resource was found that matches the request URI 'http://localhost:61057/api/JqueryAjax/PostStudent'."
  2. MessageDetail: "No action was found on the controller 'JqueryAjax' that matches the request."
<script>
    $(document).ready(function () {
        $("#btnPost").click(function () {
            alert("clicked");
            var url = 'http://localhost:61057/api/JqueryAjax/PostStudent'
            $.ajax({
                url: url,
                type: 'POST',
                dataType: 'json',
                data: { "studentname": $("#txtfirstname").val()},             
                success: function (data) {
                    console.log(data);
                },
                error: function (data) {
                    console.log(data);
                }
            });
        });
    });
</script>

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Sep 15, 2021 04:23 AM

For studentname parameter add a class with poperty and set it as parameter for your Web API.

Refer below example.

WebApi Controller

public class AjaxAPIController : ApiController
{
    [Route("api/AjaxAPI/AjaxMethod")]
    [HttpPost]
    public PersonModel AjaxMethod(MyClass person)
    {
        PersonModel personModel = new PersonModel();
        personModel.Name = person.Name;
        personModel.DateTime = DateTime.Now.ToString();
        return personModel;
    }

    public class MyClass
    {
        public string Name { get; set; }
    }
}

Controller

public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        return View();
    }
}

Model

public class PersonModel
{
    public string Name { get; set; }
    public string DateTime { get; set; }
}

View

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <input type="text" id="txtName" />
    <input type="button" id="btnGet" value="Get Current Time" />
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $("#btnGet").click(function () {
                var person = '{Name: "' + $("#txtName").val() + '" }';
                $.ajax({
                    type: "POST",
                    url: "/api/AjaxAPI/AjaxMethod",
                    data: person,
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (response) {
                        alert("Hello: " + response.Name + ".\nCurrent Date and Time: " + response.DateTime);
                    },
                    failure: function (response) {
                        alert(response.responseText);
                    },
                    error: function (response) {
                        alert(response.responseText);
                    }
                });
            });
        });
    </script>
</body>
</html>