asp.net-mvc - 如何覆盖 Web Api 方法

标签 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;
}

第二种方法是
[HttpPost]
public List<YellowPages.City> getRelevantCity(string stateID)
{
   return new Repository.YellowPages.City().getCity()
   .Where(x => x.StateID ==stateID).ToList();
}

每当我调用 getRelevantCity 方法时,都会调用 AddPersonDetails 方法,我相信这与 REST 架构有关。
现在我的问题是我该如何处理这种情况。我可以在 WebApiConfig.cs 文件中做些什么并添加约束吗?如果是,如何处理我正在使用的模型类型的约束。
谢谢。

更新 1

正如我所建议的那样,我已经更改了删除属性的方法,如下所示
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();
}

我的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');
            }
        });

    });

无论我进行哪个ajax调用,第一个方法都会被调用,你能帮我解决吗?

更新 2

我的问题很简单

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

最佳答案

进行属性路由。在您的 api 配置中,添加

config.MapHttpAttributeRoutes();

在你的 Controller 之上:
[RoutePrefix("api")]
public class ShoppingCartController....

对于 Action :
[HttpPost]
[Route("getRelevantCity")]
public List<YellowPages.Person> GetRelevantCity

[HttpPost]
[Route("addPersonDetails")]
public List<YellowPages.Person> AddPersonDetails

因此,理想情况下,您的 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)

在 $.ajax 中,更改 type: 'GET'并在参数中传递 stateId 或作为查询字符串或 api/getRelevantCity/123 其中 123 是状态 ID

关于asp.net-mvc - 如何覆盖 Web Api 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39193224/

相关文章:

asp.net-mvc - 关闭不显眼的验证运行时?

asp.net - 在 Web api IExceptionHandler 中获取请求正文

c# - .Net Web API 与路由不匹配

asp.net-mvc - Chrome34 忽略域 ".cloudapp.net"的 cookie

c# - 异步更新多选 MVC 的选项列表

c# - WebApi AuthorizationFilterAttribute : ActionArguments are empty

knockout.js - Knockout-Kendo.js DatePicker/WebAPI/ISO 8601 日期绑定(bind)

asp.net - 从mvc4迁移到mvc5,属性路由不起作用

c# - 属性路由和查询字符串

javascript - 使用ajax从数据库Mvc中的表中删除行