3

I would like for my controllers to extent endpoints based on the data-type of the same variable name. For instance method A takes an int and method B takes a string. I don't want to declare a new route but rather for the routing mechanism to differentiate between ints and strings. Here is an example of what I mean.

The "ApiControllers" setup:

public class BaseApiController: ApiController
{
        [HttpGet]
        [Route("{controller}/{id:int}")]
        public HttpResponseMessage GetEntity(int id){}
}

public class StringBaseApiController: BaseApiController
{

        [HttpGet]
        [Route("{controller}/{id:string}")]
        public HttpResponseMessage GetEntity(string id){}
}

The "WebApionfig.cs" has the following route added:

config.Routes.MapHttpRoute(
    "DefaultApi",
    "{controller}/{id}",
    new { id = RouteParameter.Optional }
);

I want to call "http://controller/1" and "http://controller/one" and get results. Instead i see the the multiple route exception.

Krolique
  • 682
  • 9
  • 14

2 Answers2

1

You could the try the below possible solutions.

//Solution #1: If the string (id) has any numeric, it will not be caught.
//Only alphabets will be caught
public class StringBaseApiController: BaseApiController
{
 [HttpGet]
 [Route("{id:alpha}")]
 public HttpResponseMessage GetEntity(string id){}
}
//Solution #2: If a seperate route for {id:Int} is already defined, then anything other than Integer will be caught here.
public class StringBaseApiController: BaseApiController
{
 [HttpGet]
 [Route("{id}")]
 public HttpResponseMessage GetEntity(string id){}
}
BipinR
  • 473
  • 7
  • 19
-2

Use only the string, and check inside if you got an int, or a string or any other thing and call the appropriate method.

public class StringBaseApiController: BaseApiController
{

        [HttpGet]
        [Route("{controller}/{id:string}")]
        public HttpResponseMessage GetEntity(string id)
        {
            int a;
            if(int.TryParse(id, out a))
            {
                return GetByInt(a);
            }
            return GetByString(id);
        }

}
dariogriffo
  • 4,148
  • 3
  • 17
  • 34
  • This isn't really helpful if the OP wants to have multiple actions, in addition it still clashes with the default route. – Ben Robinson Oct 28 '14 at 16:55
  • @BenRobinson he cant have 2 actions with the same name. Period, I offered an alternative. http://stackoverflow.com/questions/2710454/asp-net-mvc-can-i-have-multiple-names-for-the-same-action – dariogriffo Oct 28 '14 at 17:00
  • You can have 2 actions with the same route and parameter that only differ by the type of the parameter with Web API 2 and attribute routing. That link appears to be quite out of date. – Ben Robinson Oct 28 '14 at 17:04
  • @dariogriffo I can solve it this way, but I want inheritance as opposed to grouped functionality – Krolique Oct 28 '14 at 17:06
  • @dariogriffo there might be a way through using the IHttpRouteConstraint but I am not sure – Krolique Oct 28 '14 at 17:20