0

I have read many articles and similar questions but none of those decisions do not fit me. I have 3 methods:

public string GetA()
{
   return "Hello from GetA";
}
public string GetB(int id)
{
   return "Hello from GetB";
}
public string GetC(sting all)
{
   return "Hello from GetC";
}

I need to configure route like:

1.http://localhost:63087/api/Test/
2.http://localhost:63087/api/Test/all
3.http://localhost:63087/api/Test/1
4.http://localhost:63087/api/Test/1/all

How can I implement it?
I know this may be a duplicate (1, 2, 3), but I need help with it.

Thank you in advance

Community
  • 1
  • 1
  • @CliffBurton Did you read the question? I know and a wrote "I know this may be a duplicate (1, 2, 3), but I need help with it." and there are a links. Link number 3 is the same. Please read the question –  Aug 20 '15 at 08:20

4 Answers4

0

Try this code:

    public static class WebApiConfig
    {
       public static void Register(HttpConfiguration config)
       {           
                config.SuppressDefaultHostAuthentication();
                config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));

                // Web API routes
                config.MapHttpAttributeRoutes();

                config.Routes.MapHttpRoute(
                    name: "DefaultApi",
                    routeTemplate: "api/Test",
                    defaults: new { controller = "Test", action = "GetA"}
                );

                config.Routes.MapHttpRoute(
                    name: "WithID",
                    routeTemplate: "api/Test/{id}",
                    defaults: new { controller = "Test", action = "GetB", id = UrlParameter.Optional }
                );

                config.Routes.MapHttpRoute(
                    name: "ALL",
                    routeTemplate: "api/Test/all",
                    defaults: new { controller = "Test", action = "GetC"}
                );
            }
    }
Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
David Abaev
  • 690
  • 5
  • 22
  • Thanks but it is not help me. `http://localhost:58052/api/test` - Ok, `http://localhost:58052/api/test/all` - The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32'..., `http://localhost:58052/api/test/1` - Ok, `http://localhost:58052/api/test/1/all` - HTTP 404.0 - Not Found –  Aug 20 '15 at 09:52
0

Configure your routes like this

http://localhost:63087/api/Test
http://localhost:63087/api/Test/1
http://localhost:63087/api/Test/1/all

Something like this:

config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/Test",
                defaults: new { controller = "Test", action = "GetA"}
            );

            config.Routes.MapHttpRoute(
                name: "Route2",
                routeTemplate: "api/Test/{id}",
                defaults: new { controller = "Test", action = "GetB" }
            );

            config.Routes.MapHttpRoute(
                name: "Route3",
                routeTemplate: "api/Test/{id)/{all}",
                defaults: new { controller = "Test", action = "GetC"}
            );
Chanakya
  • 71
  • 4
  • No, it is not my routes. Please see the question. I have 4 routes –  Aug 20 '15 at 11:55
  • 1
    You cannot have two routes with same parameter count. Event if you have so the route engine will consider only the first route. So, both 2 and 3 cant be achieved at the same time. – Chanakya Aug 20 '15 at 13:54
  • Yes, you are right. That's why I create questions. I need a solution to this poblemy –  Aug 20 '15 at 14:03
  • 1
    Try to achieve the functionality of 2 and 4 with the same route. For `http://localhost:63087/api/Test/all` try using some default value for id (say 0 or -1) which means for all id's. Now your 2nd route becomes `http://localhost:63087/api/Test/0/{all}` and 4th becomes `http://localhost:63087/api/Test/{id}/{all}` – Chanakya Aug 20 '15 at 14:14
  • Thank you, this solution is working. What other ways can you offer me? Because 0 is not very nice –  Aug 20 '15 at 15:11
0

So, you can not have two routes with same parameter count.

2.http://localhost:63087/api/Test/all
3.http://localhost:63087/api/Test/1

In this way, you can use in all your methods parameters type string:

public string GetA()
{
   return "Hello from GetA";
}
public string GetB(string id, string all = "")
{
   if (id.Equals("all") || all.Equals("all"))
   {
       return "Hello all from GetB";
   }
   return string.Format("Hello {0} from GetB", id);
}

Route config:

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

   config.Routes.MapHttpRoute(
        name: "RouteWithParam",
        routeTemplate: "api/{controller}/{id}/{all}",
        defaults: new { all = RouteParameter.Optional }
   );
Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
0

Other solution is to add static segment to route:

1.http://localhost:63087/api/Test/
2.http://localhost:63087/api/all/Test/
3.http://localhost:63087/api/Test/1
4.http://localhost:63087/api/all/Test/1

Implementation:

    public string GetA()
    {
        return "Hello from GetA";
    }
    public string GetB(int id)
    {
        return "Hello from GetB";
    }

    [Route("api/all/{controller}/{id}")]
    [Route("api/all/{controller}")]
    public string GetC(int id= 0)
    {
        return "Hello from GetC";
    }

Route config:

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

config.Routes.MapHttpRoute(
       name: "DefaultApi",
       routeTemplate: "api/{controller}/{id}",
       defaults: new { id = RouteParameter.Optional}
);
Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116