0

I have a single ApiController: recordsController and I have the basic 4 CRUD methods in it, I need to somehow have the ability for the user to do something like:

records/users - this is achieved by the default Get method records/users/Male records/users/White

I am brand new to webapi

This is the error I am getting trying to do: http://localhost/records/Male

The request is invalid. The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.String Get(Int32)' in 'RESTServices.Controllers.recordsController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.

Pacman
  • 2,183
  • 6
  • 39
  • 70
  • Can you give more information on what exactky should happen for GET calls on the route records/users/Male or records/users/White – Ravi A. Jun 03 '16 at 16:22
  • it will return only Male users and only white users respectively – Pacman Jun 03 '16 at 16:24
  • Although not exactly but possible duplicate of [Multiple parameters in a web api 2 get](http://stackoverflow.com/questions/25161665/multiple-parameters-in-a-web-api-2-get) . All you need to have is add another GET method and have signature like this `GET(string id)` .With this the id will be either Male or White and you can fetch records accordingly. – Ravi A. Jun 03 '16 at 16:32
  • Ravi, here is my method in my controller: [Route("records/Male")] public IEnumerable GetMale(string id) { return new string[] { "value3", "value4" }; } Should my routes change or is this the only change required ? this still gives me an error – Pacman Jun 03 '16 at 16:53
  • Please remove `[Route("records/Male")]` . You don't need that, webAPI will handle the routing in this case. Also the URL will be GET http://localhost/api/records assuming the name of the API controller is RecordsController . Also can you post the routes defined in your WebApiConfig – Ravi A. Jun 03 '16 at 17:02
  • I get this error now: Multiple actions were found that match the request: Get on type RESTServices.Controllers.recordsController Get on type RESTServices.Controllers.recordsController for both records/5 and records/Male – Pacman Jun 03 '16 at 17:04
  • can you post the routes defined in your WebApiConfig – Ravi A. Jun 03 '16 at 17:06
  • the only route defined is the default one, I did not add any new routes, am I suppose to ? – Pacman Jun 03 '16 at 17:06
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/113757/discussion-between-ravi-a-and-pacman). – Ravi A. Jun 03 '16 at 17:14
  • 1
    As discussed over chat remove `GET(int id)` and change other GET method to `GET(strind id)`. – Ravi A. Jun 03 '16 at 17:28

0 Answers0