WebApi 函数调用冲突,忽略函数名
WebApi conflict calling to functions, it's ignoring the function names
我创建了一个简单的 ApiController
有 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 Class:
public class PersonController : ApiController
{
私人人物[] listPerson;
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);
}
}
}
网络Api配置:
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,所以有人能解释一下为什么以及如何解决这个问题吗?
谢谢
自定义方法命名为 asp.net web api.
路由配置默认遵循RESTful GET,POST,PUT,Delete的约定。
Custom method names in ASP.NET Web API
完成这个 post 它会解决您的问题。
应该可以使用下面的代码,只需添加{action}
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { action = "get", id = RouteParameter.Optional }
);
我创建了一个简单的 ApiController
有 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 Class:
public class PersonController : ApiController { 私人人物[] listPerson;
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);
}
}
}
网络Api配置:
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,所以有人能解释一下为什么以及如何解决这个问题吗?
谢谢
自定义方法命名为 asp.net web api.
路由配置默认遵循RESTful GET,POST,PUT,Delete的约定。
Custom method names in ASP.NET Web API
完成这个 post 它会解决您的问题。
应该可以使用下面的代码,只需添加{action}
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { action = "get", id = RouteParameter.Optional }
);