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.
UPDATE 1
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?
UPDATE 2
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
/YourController/AddPersonand/YourController/GetCity). The latter should be GET anyway.