简体   繁体   English

如何在Web API2控制器中放置多个GET方法?

[英]How to put multiple GET methods in Web API2 controller?

I am doing a Web API 2 application and I have controller named NCT_ProcessSettings and already I have two GET methods as below. 我正在做一个Web API 2应用程序,并且我有一个名为NCT_ProcessSettings控制器,并且已经有两个GET方法,如下所示。

1. public IEnumerable<Process_Settings> Get()
2. public HttpResponseMessage Get(int id)

Now I want to have third one as below (Same as first one but inside I will write different logic). 现在我想拥有下面的第三个(与第一个相同,但是在内部我将编写不同的逻辑)。

3. public IEnumerable<Process_Settings> Get() //Compiler will confuse which to pick?

I tried as below. 我尝试如下。

[HttpGet]
[Route("GetGlobalSettings")]
public IEnumerable<NCT_Process_Settings> GetGlobalSettings()
{
    return entityObject.NCT_Process_Settings.Where(c => c.project_id == 0).ToList();
}

Below is my angularcode to call api. 下面是我调用api的angularcode。

 var url = '/api/NCT_ProcessSettings/GetGlobalSettings';

May I have some idea how to fix this? 我可以知道如何解决这个问题吗? Any help would be appreciated? 任何帮助,将不胜感激?

Enable attribute routing in WebApiConfig.cs before convention-based routes. 在基于约定的路由之前,在WebApiConfig.cs中启用属性路由。

config.MapHttpAttributeRoutes();

Next update controller to use routing attributes. 下一步更新控制器以使用路由属性。 (note the route prefix) (请注意路由前缀)

[RoutePrefix("api/NCT_ProcessSettings")]
public class NCT_ProcessSettingsController : ApiController {

    //GET api/NCT_ProcessSettings
    [HttpGet]
    [Route("")]
    public IEnumerable<Process_Settings> Get() { ... }

    //GET api/NCT_ProcessSettings/5
    [HttpGet]
    [Route("{id:int}")]
    public HttpResponseMessage Get(int id) { ... }

    //GET api/NCT_ProcessSettings/GetGlobalSettings
    [HttpGet]
    [Route("GetGlobalSettings")]
    public IEnumerable<NCT_Process_Settings> GetGlobalSettings() { ... }

}

Read up more documentation here Attribute Routing in ASP.NET Web API 2 在此处阅读更多文档。ASP.NET Web API 2中的属性路由

Used Action Name attribute 使用的动作名称属性

       [ActionName("Get")]
       public IEnumerable<Process_Settings> Get1()//used any name here
       {
       }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM