0

I want to create a controller that can respond to those requests:

  1. api/User/123
  2. api/User/UsersOfCompany?id=123&pageIndex=0&pageSize=20
  3. api/User/AllowedCompanies?id=123&pageIndex=0&pageSize=10

This is my actual code.

User Controller

public class UserController : ApiController
{
    [HttpGet]
    public IHttpActionResult Get(int id)
    {
        //... implementation...
    }

    [HttpGet]
    [ActionName("UsersOfCompany")]
    public IHttpActionResult UsersOfCompany(int id, [FromUri] Paging paging)
    {
        //... implementation...
    }

    [HttpGet]
    [ActionName("AllowedCompanies")]
    public IHttpActionResult AllowedCompanies(int id, [FromUri] Paging paging)
    {
        //... implementation...
    }
}

WebApiConfig

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Servizi e configurazione dell'API Web

        // Route dell'API Web
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "ApiWithActionName",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        config.MessageHandlers.Add(new LanguageMessageHandler());
    }
}

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

Based on sample request above, I would like routing work this way:

  1. Get(int id)
  2. UsersOfCompany(int id, [FromUri] Paging paging)
  3. AllowedCompanies(int id, [FromUri] Paging paging)

But, now I can only call the last 2 requests (UsersOfCompany, AllowedCompanies). When I call api/User/123 I get this error:

"Could not find an HTTP resource that matches the request's URL 'Service/api/User/123'.", "Unable to find an action on the 'User' controller with name '42887'."

I already tried to invert the order of route registration but I otained the opposite behavior, so that api/User/123 call works and the others two not.

Where's the problem?

2
  • 2
    What is the problem that you are asking us to find? Is there an error message? If so, when or where does it occur? Commented Oct 18, 2017 at 15:08
  • Sorry... Updated the question! :-) Commented Oct 18, 2017 at 15:49

1 Answer 1

3

Your error message clearly states that WebApi tries to match Service/api/User/123 to the first pattern: api/{controller}/{action}/{id}.

You could try to change order of registration in your WebApiConfig or to specify the route explicitly:

[Route("api/User/{id:int}")]
[HttpGet]
public IHttpActionResult Get(int id)
{
    //... implementation...
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.