I am calling Asp.net web api Post methos which has one parameter named value of type string.
Code as below:
In ValueController:
public IHttpActionResult Post(string value)
{
return Created(Request.RequestUri + "/" + "id=5", value);
}
Jquery Calling Web Api:
$.ajax({
url: 'http://localhost:63022/api/values',
type: 'POST',
data: '{"value":"ravi"}',
contentType: "application/json;charset=utf-8",
crossDomain : true,
success: function (data) {
alert('success');
},
error: function (x, y, z) {
alert('error');
}
});
Question 1:
When I run this code and call web api Post method using above jquery ajax call, I get Http error 405 method not allowed.
Question 2:
Modification in ValueController, adding FromBody attribute
public IHttpActionResult Post([FromBody]string value)
{
return Created(Request.RequestUri + "/" + "id=5", value);
}
When I run this code and call web api Post method using above jquery ajax call, I get null in string value action method parameter [Post([FromBody]
string value)].
Is it mandatory to write FromBody attribute for Post method to work in Question 1?
Guys please help me out for question 1 and 2...