C# Api 控制器声明多个 Get 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10121152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 12:30:36  来源:igfitidea点击:

Api controller declaring more than one Get statement

c#asp.net-mvc-4asp.net-web-apiasp.net-routing

提问by Neil Knight

Using the new Api Controller in MVC4, and I've found a problem. If I have the following methods:

在 MVC4 中使用新的 Api 控制器,我发现了一个问题。如果我有以下方法:

public IEnumberable<string> GetAll()

public IEnumberable<string> GetAll()

public IEnumberable<string> GetSpecific(int i)

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 GetAllmethod, even though the $.getJSONis set to the GetAllIntegersmethod:

这将起作用。但是,如果我想检索一些不同类型的不同数据,它默认为该GetAll方法,即使$.getJSON设置为该GetAllIntegers方法:

public IEnumberable<int> GetAllIntergers()

public IEnumberable<int> GetAllIntergers()

(bad naming conventions)

(错误的命名约定)

Is it possible for me to be able to do this?

我有可能做到这一点吗?

Can I only have a single GetAllmethod in the Web API controller?

我可以GetAll在 Web API 控制器中只有一个方法吗?

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:

我认为将我想要实现的目标形象化更容易。这是一段代码,用于显示我希望能够在单个中执行的操作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
}

采纳答案by tpeczek

This is all in the routing. The default Web API route looks like this:

这一切都在路由中。默认的 Web API 路由如下所示:

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 GetAllit can find. To work around this you need to define a route where the action name is included:

使用默认路由模板,Web API 使用 HTTP 方法来选择操作。结果它会将一个没有参数的 GET 请求映射到GetAll它可以找到的第一个。要解决此问题,您需要定义一个包含操作名称的路由:

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:

之后,您可以使用以下 URL 发出请求:

  • api/yourapicontroller/GetClients
  • api/yourapicontroller/GetStaffMembers
  • api/yourapicontroller/getclients
  • api/yourapicontroller/GetStaffMembers

This way you can have multiple GetAllin Controller.

这样你就可以GetAll在 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]).

这里更重要的一点是,对于这种路由风格,您必须使用属性来指定允许的 HTTP 方法(如 [HttpGet])。

There is also an option of mixing the default Web API verb based routing with traditional approach, it is very well described here:

还有一个选项是将基于默认 Web API 动词的路由与传统方法混合使用,这里有很好的描述:

回答by Archna

In case someone else faces this problem. Here's how I solved this. Use the [Route] attribute on your controller to route to a specific url.

万一其他人面临这个问题。这是我解决这个问题的方法。使用控制器上的 [Route] 属性路由到特定 url。

[Route("api/getClient")]
public ClientViewModel GetClient(int id)

[Route("api/getAllClients")]
public IEnumerable<ClientViewModel> GetClients()