0
votes

I have the following controller:

public class CustomerController : ApiController
{
    private ICustomerService customerService;

    public CustomerController(ICustomerService customerService)
    {
        this.customerService= customerService;
    }

    public IEnumerable<Customer> GetAll()
    {
        return customerService.GetAll();
    }

    [HttpGet]
    public Customer GetCustomer(int id)
    {
        //Get customer code...
        return customer;
    }

    [ActionName("Save")]
    [AcceptVerbs("PUT")]
    [HttpPost]
    public int SaveCustomer(Customer customer)
    {
        //Save customer code...
        return customer.id;
    }

    [ActionName("Test")]
    [HttpGet]
    public string TestCustomer()
    {
        return "test";
    }

    [HttpDelete]
    public bool DeleteCustomer(int id)
    {
        //Delete customer code...
        return false;
    }
}

I have the following default RouteConfig:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}

And the following default WebApiConfig:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        config.EnableSystemDiagnosticsTracing();
    }
}

When I try ~api/Customer I get the following error: Multiple actions were found that match the request: System.Collections.Generic.IEnumerable`1[Customer] GetAll() on type MyApp.API.Controllers.PriceLevelController System.String TestCustomer() on type MyApp.API.Controllers.CustomerController

What changes to my route config do I need to do so that my default methods as well as my custom methods work when called from the client?

1

1 Answers

0
votes

I think this error comes when you have multiple get method with same param in your controller. you have to write another Routes in your config file.

You can refer this link