Andrew Simpson
Andrew Simpson

Reputation: 7344

Forcing a Route to my API

I have these 2 apis in my API Controller (MVC5):

    [HttpGet]
    [HttpPost]
    public RestErrorHandler Add([FromBody]Models.Customer customer)
    {
        try
        {
          //do something...
            return new RestErrorHandler { Error = res.ErrorMessage, Location = MethodBase.GetCurrentMethod().Name };
        }
        catch (Exception ex)
        {
            return new RestErrorHandler { Error = ex.ToString(), Location = MethodBase.GetCurrentMethod().Name };
        }
    }

    [HttpGet]
    [HttpPost]
    public RestErrorHandler Delete([FromBody]Models.Customer customer)
    {
        try
        {
          //do something
            return new RestErrorHandler { Error = res.ErrorMessage, Location = MethodBase.GetCurrentMethod().Name };
        }
        catch (Exception ex)
        {
            return new RestErrorHandler { Error = ex.ToString(), Location = MethodBase.GetCurrentMethod().Name };
        }
    }

My client call is this:

var response = client.PostAsync(string.Format("http://my_uri/api/Customer/" + action), contentPost).Result;

Where 'action' can be 'Add' or 'Delete'.

Testing the delete it tells me this error:

Multiple actions were found that match the request: 

To try and 'force' it to go to the correct way I added a Route tag to my method in my API:

    [HttpGet]
    [HttpPost]
    [Route("Customer/Delete/Customer")]
    public RestErrorHandler Delete([FromBody]Models.Customer customer)
    {
        try
        {
            //do something
            return new RestErrorHandler { Error = res.ErrorMessage, Location = MethodBase.GetCurrentMethod().Name };
        }
        catch (Exception ex)
        {
            return new RestErrorHandler { Error = ex.ToString(), Location = MethodBase.GetCurrentMethod().Name };
        }
    }

But it calls my 'Add' api..?

What am I missing please?

Upvotes: 0

Views: 81

Answers (1)

Stephen Brickner
Stephen Brickner

Reputation: 2602

Both of these methods are POST's, remove GET.

[RoutePrefix("api")]
public class YourController: ApiController
{
    [HttpPost, Route("customers/add")]
    public RestErrorHandler Add([FromBody]Models.Customer customer)
    {
            try
            {
              //do something...
                return new RestErrorHandler { Error = res.ErrorMessage, Location = MethodBase.GetCurrentMethod().Name };
            }
            catch (Exception ex)
            {
                return new RestErrorHandler { Error = ex.ToString(), Location = MethodBase.GetCurrentMethod().Name };
            }
    }

    [HttpPost, Route("customers/remove")]
    public RestErrorHandler Delete([FromBody]Models.Customer customer)
    {
            try
            {
                //do something
                return new RestErrorHandler { Error = res.ErrorMessage, Location = MethodBase.GetCurrentMethod().Name };
            }
            catch (Exception ex)
            {
                return new RestErrorHandler { Error = ex.ToString(), Location = MethodBase.GetCurrentMethod().Name };
            }
    }
}

public void Configuration(IAppBuilder app)
{
     var configuration = new HttpConfiguration();

     //for route attributes on controllers
     configuration.MapHttpAttributeRoutes();
}

var response = await client.PostAsync("http://my_uri/api/customers/add", contentPost);

Postman enter image description here

Upvotes: 1

Related Questions