0
votes

I'm trying to add attribute routing to a ApiController like so:

public class PhoneNumbersController : ApiController
{
    // GET BY ID
    public PhoneNumber GetById(int id)
    {
        return PhoneNumbersSelect(id)[0];
    }

    // GET BY ID TypeOFPhoneNumbers/Id
    [Route("api/typeOfPhoneNumbers/{id}")]
    public TypeOfPhoneNumber GetTypeOfPhoneNumberById(int id)
    {
        return TypeOfPhoneNumbersSelect(id)[0];
    }
}

My config looks like this:

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

I'm getting a 500 Error when trying to call api/phoneNumbers/1. If I comment out the GetTypeOfPhoneNumbersId() function the default controller GetById() works fine. What am I doing wrong? Am I not allowed to declare a unique route in the ApiController because of the way the config is set up?

Also, as I just found out, calling the api/typeOfPhoneNumbers/1 returns a 404 error, so that routing doesn't work at all.

Thanks for any help!

2
how are the routes configured? - harishr
I know this is old but...[Route("~/api/typeOfPhoneNumbers/{id}")] - Jon49

2 Answers

0
votes

I believe you miss the controller name (phoneNumbers) in your route, this is a working code (I've tested it)

public class PhoneNumbersController : ApiController
{
    // GET BY ID
    [HttpGet]
    public PhoneNumber GetById(int id)
    {
        return PhoneNumbersSelect(id)[0];
    }

    // GET BY ID TypeOFPhoneNumbers/Id
    [HttpGet]
    [Route("api/phoneNumbers/typeOfPhoneNumbers/{id:int}")]
    public TypeOfPhoneNumber GetTypeOfPhoneNumberById(int id)
    {
        return TypeOfPhoneNumbersSelect(id)[0];
    }
}
0
votes

Could you try to access TypeOFPhoneNumbers's resource like that : ~api/typeOfPhoneNumbers?id=1