Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
160 views
in Technique[技术] by (71.8m points)

c# - Api controller declaring more than one Get statement

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
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...