I have this AJAX call:
let errorModel = {
exception: exception,
extraMessage: extraMessage,
time: Date.now()
}
$.ajax({
url: "/api/error/LogError",
type: "POST",
data: errorModel
});
And this web api 2 method here in this controller
[RoutePrefix("api/Error")]
public class ErrorController : ApiController
{
[HttpPost]
[Route("LogError")]
public IHttpActionResult LogError([FromBody]ErrorModel errorModel)
{
try
{
_db.ErrorModels.Add(errorModel);
_db.SaveChanges();
return Created(new Uri(Request.RequestUri + "/" + errorModel.Id), errorModel);
}
catch (Exception e)
{
return BadRequest(e.Message);
}
}
}
But when my code reaches the ajax call, I get an error message like this:
{
"message": "The requested resource does not support http method 'GET'."
}
What am I doing wrong here? I have plenty of api calls like this, and all of them work.
I have tested this code and found it working fine.
The error message you posted seems indicate that you are using 'get' method in ajax instead of 'post' method.
I have found the result from popular REST-client 'postman'.
Note: I have used string data-type for all of the properties of ErrorModel
Hi all sorry for taking your time it seems it was a stupid error on my side, it seems my javascript Date.Now() wasn't returning what i expected and the api method received it as 00/00/0001 which was denied by the SQL server. Am not sure why the error message was saying what it was but after i fixed that by setting datetime in the api call the error resolved it self. Thanks all for your time