I have a web api method in a web api controller as below which is working perfectly..
[Route("api/myquery")]
[HttpPost]
public HttpResponseMessage MyQuery([FromBody] string id)
{
if (string.IsNullOrEmpty(id))
{
return new HttpResponseMessage(HttpStatusCode.BadRequest);
}
return new HttpResponseMessage(HttpStatusCode.OK);
}
I would like to use a similar one in my asp.net mvc controller..
I tried the following..
[HttpPost]
public ActionResult MyQuery(string id)
{
return this.Content("");
}
The data posted is sent from a same method in javascript.
$.ajax("/api/myquery", {
data: JSON.stringify(tmp1),
type: "post", contentType: "application/json",
success: function (result) { alert("Saved Successfully") },
error: function (result) { alert("Error Saving") }
});
But, as MVC does not have a [FromBody] tag I'm unable to access the content being sent.. though the method is being called, id always shows null...
tmp1is{ id: someValue }then you need to specify the correct url -url: '/yourControllerName/myquery',or betterurl: '@Url.Action("myquery", "yourControllerName")',tmp1is{ id: someValue }?