I am new to WebApi and right now i have two httpPost method like this
[HttpPost]
public List<YellowPages.Person> AddPersonDetails(YellowPages.Person person)
{
Repository.Repository.personsList.Add(person);
return Repository.Repository.personsList;
}
and the second method is
[HttpPost]
public List<YellowPages.City> getRelevantCity(string stateID)
{
return new Repository.YellowPages.City().getCity()
.Where(x => x.StateID ==stateID).ToList();
}
whenever i make a call to the getRelevantCity method, AddPersonDetails method gets called, i believe this is something to do with the REST architecture. now my question is how can i handle this situation.Is there anything i can do in the WebApiConfig.cs file and add constraints? if yes, how to handle constraints for the model type which i am using. Thank you.
as suggested i have changed my both the method removing the attributes as follows
public List<YellowPages.Person> AddPersonDetails(YellowPages.Person person)
{
Repository.Repository.personsList.Add(person);
return Repository.Repository.personsList;
}
public List<YellowPages.City> getRelevantCity(string stateID)
{
return new Repository.YellowPages.City().getCity().Where(x => x.StateID == stateID).ToList();
}
my ajax call is like this
$('#StateID').change(function () {
$.ajax({
type: 'POST',
url: '/api/shoppingCart/getRelevantCity',
ContentType: 'application/json',
data: { 'stateID': $('#StateID').val() },
dataType: 'json',
success: function (returnData) {
var grid = '';
$.each(returnData, function (i, d) {
grid = grid + createDom(d);
});
$('#result').empty().append(
grid
);
},
error: function (xhr, ajaxOptions, thrownError) {
alert('error');
}
});
});
$('#btnAjax').click(function (e) {
debugger;
e.preventDefault();
var d = { 'PersonName': $('#PersonName').val(), 'gender': $('#gender').prop('checked', true).val(), 'StreetAddress': $('#StreetAddress').val(), 'StateID': $("#StateID option:selected").text(), 'Pincode': $('#Pincode').val() };
$.ajax({
type: 'POST',
url: '/api/shoppingCart/AddPersonDetails',
ContentType: 'application/json',
data: d,
dataType: 'json',
success: function (returnData) {
var grid = '';
$.each(returnData, function (i, d) {
grid = grid + createDom(d);
});
$('#result').empty().append(
grid
);
},
error: function (xhr, ajaxOptions, thrownError) {
alert('error');
}
});
});
no matter which ajax call i make, the first method gets called, can you help me how to hendle it?
my question is simple
Suppose if i have 4 GET methods in my webApi, then how can I handle the webApi so that i could get all the 4 GET methods implemented
A method used as a controller action cannot be overloaded..
In C#, there might be two or more methods in a class with the same name but different numbers, types, and order of parameters, it is called method overloading.
Go for attribute routing. In your api config, add
config.MapHttpAttributeRoutes();
And on top of your controller:
[RoutePrefix("api")]
public class ShoppingCartController....
And for actions:
[HttpPost]
[Route("getRelevantCity")]
public List<YellowPages.Person> GetRelevantCity
[HttpPost]
[Route("addPersonDetails")]
public List<YellowPages.Person> AddPersonDetails
Thus said, your GetRelevantCity ideally should be a GET method and not a POST. I would suggest you to change that to HttpGet in action as well as in your javascript code:
[HttpGet] //this is not required; as per naming convention this will be a GET request by default
[Route("getRelevantCity/{stateId}")]
public List<YellowPages.Person> GetRelevantCity(int stateId)
And in $.ajax, change type: 'GET'
and pass stateId in params or as query string or api/getRelevantCity/123 where 123 is the state id
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With