【问题标题】:WebApi conflict calling to functions, it's ignoring the function namesWebApi 冲突调用函数,它忽略了函数名
【发布时间】:2015-05-21 14:44:42
【问题描述】:

我创建了一个简单的ApiController whit 3 函数。

  • get() // 全部获取
  • get(int id) // 通过 id 获取
  • getFirst() // 获取列表中的第一个

我有 2 个场景,我会非常感谢一个正确的解释。

  • 1:如果我只实现 get()get(int id),当我调用 api/controller/{id}(id 可选)时,它会完美运行

  • 2:当我尝试访问api/controller/function/{id}(id 可选)时,当我拥有 3 个功能时,它不起作用。仅适用于通过 id 获取的功能。您可以在下面看到我遇到的错误。

    请求无效。 参数字典包含“Practica.Controllers.PersonController”中方法“Practica.Models.Person GetById(Int32)”的不可空类型“System.Int32”的参数“id”的空条目。可选参数必须是引用类型、可空类型或声明为可选参数。

API 类:

公共类 PersonController : ApiController { 私人人员[] 列表人员;

    public PersonController() {
        listPerson = new Person[3];
        listPerson[0] = new Person { 
            id = 1,
            firstName = "test",
            lastName = "test",
            email = "test@gmail.com",
            age = 26
        };
        listPerson[1] = new Person
        {
            id = 2,
            firstName = "test",
            lastName = "test",
            email = "test@gmail.com",
            age = 26
        };
        listPerson[2] = new Person
        {
            id = 3,
            firstName = "test",
            lastName = "test",
            email = "test@gmail.com",
            age = 26
        };
    }

    public Person GetById(int id) {
        try
        {
            var person = this.listPerson.Where(p => p.id == id).FirstOrDefault();
            if (person != null)
            {
                return person;
            }
            else {
                throw new InvalidOperationException();
            }
        }
        catch (Exception) {
            throw new HttpResponseException(HttpStatusCode.NotFound);
        }
    }

    public Person[] Get()
    {
        try
        {
            if (this.listPerson != null)
            {
                return this.listPerson;
            }
            else
            {
                throw new InvalidOperationException();
            }
        }
        catch (Exception)
        {
            throw new HttpResponseException(HttpStatusCode.NotFound);
        }
    }

    public Person GetFirst()
    {
        try
        {
            if (this.listPerson != null)
            {
                return this.listPerson.Where(x=>x.Equals(1)).FirstOrDefault();
            }
            else
            {
                throw new InvalidOperationException();
            }
        }
        catch (Exception)
        {
            throw new HttpResponseException(HttpStatusCode.NotFound);
        }
    }
}

WebApiConfig:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}

就像ApiController 只识别get 和get?id,所以有人可以解释一下为什么以及如何解决这个问题?

谢谢

【问题讨论】:

标签: c# api asp.net-web-api routing


【解决方案1】:

自定义方法命名为 asp.net web api。

默认情况下,路由配置遵循 GET、POST、PUT、Delete 的 RESTful 约定。

Custom method names in ASP.NET Web API

阅读这篇文章它会解决你的问题。

应该使用下面的代码,只需添加 {action}

routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{action}/{id}",
    defaults: new { action = "get", id = RouteParameter.Optional }
);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-26
    • 1970-01-01
    • 2012-09-15
    • 2016-05-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多