Metode kustom perutean API Web

Saya memiliki pengontrol berikut:

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;
    }
}

Saya memiliki RouteConfig default berikut:

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 }
        );
    }
}

Dan WebApiConfig default berikut:

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();
    }
}

Ketika saya mencoba ~api/Customer saya mendapatkan kesalahan berikut: Ditemukan beberapa tindakan yang cocok dengan permintaan: System.Collections.Generic.IEnumerable`1[Customer] GetAll() pada tipe MyApp.API.Controllers.PriceLevelController System.String TestCustomer () pada tipe MyApp.API.Controllers.CustomerController

Perubahan apa pada konfigurasi rute yang perlu saya lakukan agar metode default dan metode kustom saya berfungsi saat dipanggil dari klien?


person Ivan-Mark Debono    schedule 19.11.2013    source sumber


Jawaban (1)


Saya pikir kesalahan ini terjadi ketika Anda memiliki beberapa metode get dengan param yang sama di pengontrol Anda. Anda harus menulis Rute lain di file konfigurasi Anda.

Anda dapat merujuk tautan

person Andy    schedule 19.11.2013