API控制器宣布多个Get语句 [英] Api controller declaring more than one Get statement
问题描述
使用MVC4新的API控制,我发现一个问题。如果我有以下几种方法:
公共IEnumberable<串GT; GETALL()
公共IEnumberable<串GT; GetSpecific(int i)以
这会工作。但是,如果我想要检索不同类型的一些不同的数据,则默认为 GETALL
方法,即使 $。的getJSON
设置为 GetAllIntegers
方法:
公共IEnumberable< INT> GetAllIntergers()
的(坏的命名约定)的
是否有可能对我来说,能做到这一点?
我也只能有在Web API控制器单 GETALL
方法?
我觉得它更容易想象我想要实现的。这里是code的一个片段,显示想我是能够做到的,在一个单一的 ApiController
:
公开的IEnumerable<串GT; GetClients()
{//获取数据
}公共IEnumerable的<串GT; GetClient(INT ID)
{//获取数据
}公共IEnumerable的<串GT; GetStaffMember(INT ID)
{//获取数据
}公共IEnumerable的<串GT; GetStaffMembers()
{//获取数据
}
这是所有路由。默认的Web API的路线是这样的:
config.Routes.MapHttpRoute(
名称:API默认,
routeTemplate:API / {}控制器/(编号),
默认:新{ID = RouteParameter.Optional}
);
使用默认的路由模板,网页API使用HTTP方法来选择操作。在结果它将映射不带参数的第一个 GETALL
,它可以找到一个GET请求。要解决这一点,你需要定义包含在操作名称路线:
config.Routes.MapHttpRoute(
名称:ActionApi
routeTemplate:API / {控制器} / {行动} / {ID}
默认:新{ID = RouteParameter.Optional}
);
之后,可以使明星与下列URL的请求:
- API / yourapicontroller / GetClients
- API / yourapicontroller / GetStaffMembers
这样你可以有多个 GETALL
的控制器。
这里的一个更重要的是,这种风格的路由,则必须使用属性来指定允许的HTTP方法(如[HTTPGET])。
有也与传统方法默认的Web API基于动词路由的混合选项,这是很好的描述如下:
Using the new Api Controller in MVC4, and I've found a problem. If I have the following methods:
public IEnumberable<string> GetAll()
public IEnumberable<string> GetSpecific(int i)
This will work. However, if I want to retrieve some different data of a different type, it defaults to the GetAll
method, even though the $.getJSON
is set to the GetAllIntegers
method:
public IEnumberable<int> GetAllIntergers()
(bad naming conventions)
Is it possible for me to be able to do this?
Can I only have a single GetAll
method in the Web API controller?
I think it's easier to visualise what I'm trying to achieve. Here is a snippet of code to show what I'd like to be able to do, in a single ApiController
:
public IEnumerable<string> GetClients()
{ // Get data
}
public IEnumerable<string> GetClient(int id)
{ // Get data
}
public IEnumerable<string> GetStaffMember(int id)
{ // Get data
}
public IEnumerable<string> GetStaffMembers()
{ // Get data
}
This is all in the routing. The default Web API route looks like this:
config.Routes.MapHttpRoute(
name: "API Default",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
With the default routing template, Web API uses the HTTP method to select the action. In result it will map a GET request with no parameters to first GetAll
it can find. To work around this you need to define a route where the action name is included:
config.Routes.MapHttpRoute(
name: "ActionApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
After that you can star making requests with following URL's:
- api/yourapicontroller/GetClients
- api/yourapicontroller/GetStaffMembers
This way you can have multiple GetAll
in Controller.
One more important thing here is that with this style of routing, you must use attributes to specify the allowed HTTP methods (like [HttpGet]).
There is also an option of mixing the default Web API verb based routing with traditional approach, it is very well described here:
这篇关于API控制器宣布多个Get语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!