3

I have two controllers one named "Products" and the other "ProductsGroup"

[RoutePrefix("api/{clientUrl}/products")]
public class ProductsController : BaseApiController
{
    /// <summary>
    /// Get all products from a client
    /// </summary>
    /// <returns></returns>
    [Route("")]
    public HttpResponseMessage Get()
    {
        var model = Repository.GetProducts(ClientId).Select(p => ModelFactory.Create<ProductsModel>(p));
        return Request.CreateResponse(HttpStatusCode.OK, model);
    }
}

[RoutePrefix("api/{clientUrl}/products/groups")]
public class ProductGroupsController : BaseApiController
{
    /// <summary>
    /// Get all productgroups
    /// </summary>
    /// <returns></returns>
    [Route("")]
    public HttpResponseMessage Get()
    {
        var model = Repository.GetProductGroups(ClientId);
        return Request.CreateResponse(HttpStatusCode.OK, model);
    }
}

When i route like this the ProductGroupsController i not accessable due to "Multiple controller types were found that match the URL"

Is it possible to make the routing ignore the "products" part of the url and only map to the productgroupscontroller?

1 Answer 1

5

Consider using single controller for both:

[RoutePrefix("api/{clientUrl}/products")]
public class ProductsController : BaseApiController
{
    [Route("")]
    public HttpResponseMessage GetProducts()  {}

    [Route("groups")]
    public HttpResponseMessage GetProductGroups()  {}
}

Or don't use RoutePrefixes:

public class ProductsController : BaseApiController
{
    [Route("api/{clientUrl}/products")]
    public HttpResponseMessage Get()  {}
}

public class ProductGroupsController : BaseApiController
{
    [Route("api/{clientUrl}/products/groups")]
    public HttpResponseMessage Get()    {  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Well, i'll go for the single controller then. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.