Lij*_*raj 11 asp.net-mvc asp.net-web-api asp.net-web-api-routing
我是WebApi的新手,现在我有两个像这样的httpPost方法
[HttpPost]
public List<YellowPages.Person> AddPersonDetails(YellowPages.Person person)
{
Repository.Repository.personsList.Add(person);
return Repository.Repository.personsList;
}
Run Code Online (Sandbox Code Playgroud)
第二种方法是
[HttpPost]
public List<YellowPages.City> getRelevantCity(string stateID)
{
return new Repository.YellowPages.City().getCity()
.Where(x => x.StateID ==stateID).ToList();
}
Run Code Online (Sandbox Code Playgroud)
每当我调用getRelevantCity方法时,都会调用AddPersonDetails方法,我相信这与REST架构有关.现在我的问题是如何处理这种情况.我在WebApiConfig.cs文件中有什么可以做的并添加约束吗?如果是,如何处理我正在使用的模型类型的约束.谢谢.
如我所知,我已经改变了我删除属性的方法,如下所示
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();
}
Run Code Online (Sandbox Code Playgroud)
我的ajax电话是这样的
$('#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');
}
});
});
Run Code Online (Sandbox Code Playgroud)
无论我拨打哪个ajax电话,第一种方法都会被调用,你能帮我解决这个问题吗?
我的问题很简单
假设我的webApi中有4个GET方法,那么如何处理webApi以便我可以实现所有4个GET方法
Dev*_*per 15
去寻找属性路由.在你的api配置中,添加
config.MapHttpAttributeRoutes();
Run Code Online (Sandbox Code Playgroud)
在你的控制器上:
[RoutePrefix("api")]
public class ShoppingCartController....
Run Code Online (Sandbox Code Playgroud)
对于行动:
[HttpPost]
[Route("getRelevantCity")]
public List<YellowPages.Person> GetRelevantCity
[HttpPost]
[Route("addPersonDetails")]
public List<YellowPages.Person> AddPersonDetails
Run Code Online (Sandbox Code Playgroud)
如上所述,理想情况下,您的GetRelevantCity应该是GET方法而不是POST.我建议你将其改为HttpGet,以及你的javascript代码:
[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)
Run Code Online (Sandbox Code Playgroud)
并在$ .ajax中,更改type: 'GET'并传递params中的stateId或查询字符串或api/getRelevantCity/123,其中123是状态ID
| 归档时间: |
|
| 查看次数: |
1193 次 |
| 最近记录: |