1

I am doing a Web API 2 application and I have controller named NCT_ProcessSettings and already I have two GET methods as below.

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.

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

May I have some idea how to fix this? Any help would be appreciated?

Nkosi
  • 235,767
  • 35
  • 427
  • 472
Niranjan Godbole
  • 2,135
  • 7
  • 43
  • 90

2 Answers2

4

Enable attribute routing in WebApiConfig.cs before convention-based routes.

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

Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • Thank you, So if i want to call any API then localhost:22045/api/NCT_ProcessSettings/GetGlobalSettings in this way right? – Niranjan Godbole Feb 10 '17 at 14:07
  • For that controller yes. Look at the example comments above each action in the answer, which shows what the request for that action would look like. You should also take some time and go through the web api documentations. – Nkosi Feb 10 '17 at 14:08
1

Used Action Name attribute

       [ActionName("Get")]
       public IEnumerable<Process_Settings> Get1()//used any name here
       {
       }
Amol B Lande
  • 242
  • 1
  • 2
  • 13