Reputation: 1
I am making a POST request. The method parameters are null
. I have verified the call and it seems fine to me. Any suggestions appreciated.
var WebServiceURL = 'https://localhost:44341/api/controllername/ForwardInfo'
$.ajax({
type: webServiceRequestMethod,
url: WebServiceURL,
data: JSON.stringify(objSendData),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {}
failure: function() {}
})
[ApiController]
[Route("api/Ask4Purple/FowardInfo")]
public IActionResult FowardInfo(string zipcode, int searchRadius, string ProductCodes)
//public IActionResult FowardInfo([FromBody] ListOfStores value)
{
string Zipcode = zipcode;
int SearchRadius = searchRadius;
string ProductCode = ProductCodes;
// ...
}
Upvotes: 0
Views: 265
Reputation: 20116
You need to set the POST
method receive a viewModel which contains all those properties:
public class MyViewModel
{
public string zipcode { get; set; }
public int searchRadius { get; set; }
public string ProductCodes { get; set; }
}
Besides, maybe you have a typo in your OP that your use FowardInfo
on route attribute while js url uses ForwardInfo
.
Below is a working demo:
1.Ajax
var WebServiceURL = 'https://localhost:44341/api/Ask4Purple/ForwardInfo';
var objSendData = {
zipcode: "123",
searchRadius: 5,
ProductCodes: "001"
};
$.ajax({
type: "POST",
url: WebServiceURL,
data: JSON.stringify(objSendData),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) { }
})
2. Controller
[Route("api/[controller]")]
[ApiController]
public class Ask4PurpleController : ControllerBase
{
[HttpPost("ForwardInfo")]
public IActionResult ForwardInfo([FromBody] MyViewModel value)
{
string Zipcode = value.zipcode;
int SearchRadius = value.searchRadius;
string ProductCode = value.ProductCodes;
// ...
return new JsonResult(value);
}
}
3.Result:
Upvotes: 1