Search code examples
asp.net-mvcrestrestful-urlwcf-rest

More than one REST service with similar parameters for both request and responses


I would like to create more than one C# webAPI REST service that look similar.

# For validating login
http://localhost:51055/api/Login/

# For, let's say, something else.
http://localhost:51055/api/Login/

And my controller looks like this:

[HttpPost]
public LoginData Get(LoginData loginData)
{
  // Do task A
  //return an obj of type LoginData
}

[HttpPost]
public LoginData Get2(LoginData loginData)
{
  // Do task B
  //return an obj of type LoginData
}

As you can see, I have two services pretty nearly similar... taking similar request parameters and providing similar response parameters.

How can I differentiate which service to call? Is there a way to force/specify which service to call?


Solution

  • Use [Route("NameOfAction")] above your controller method. It maps incoming URL requests to actions in your controller.

    [HttpPost]
    [Route("Get1")]
    public LoginData Get(LoginData loginData)
    {
        // Do task A
        //return an obj of type LoginData
    }
    
    [HttpPost]
    [Route("Get2")]
    public LoginData Get2(LoginData loginData)
    {
        // Do task B
        //return an obj of type LoginData
    }
    

    The API call will be http://localhost:51055/api/Login/Get1 and http://localhost:51055/api/Login/Get2.