In a simple WebApi project, I want to make calls to either return an individual score by score id (int), or all scores for a particular patient (by patient name = string).
// GET api/Scores/GetScores/patientname
[HttpGet]
public IEnumerable<Score> GetScores(string pat_id)
{
return db.Scores.Where(model => model.PatientID==pat_id).AsEnumerable();
}
//GET api/GetScores/GetScore/5
[HttpGet]
public Score GetScore(int id)
{
Score score = db.Scores.Find(id);
if (score == null)
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
}
return score;
}
If I have RouteDebugger enabled, then a URL of 'http://localhost:64789/api/Scores/GetScore/8' shows a match to the second route an ID of 8, and a URL of 'http://localhost:64789/api/Scores/GetScores/johnsmith' matches the first route with an id of johmsmith.
If I disable RouteDebugger, both URLs fails with an error like "No HTTP resource was found that matches the request URI 'http://localhost:64789/api/Scores/GetScore/8'.","MessageDetail":"No route providing a controller name was found to match request URI
'http://localhost:64789/api/Scores/GetScore/8'"}
I can comment out the GetScores(string pat_id) controller method, in which case the URL 'http://localhost:64789/api/Scores/GetScore/8' works, and returns the expected result. However, the reverse case, commenting out the GetScore(int id) method does NOT
work, and the URL 'http://localhost:64789/api/Scores/GetScores/johnsmith still returns the same error - "No HTTP resource was found that matches the request URI 'http://localhost:64789/api/Scores/GetScores/johnsmith.","MessageDetail":"No route providing a
controller name was found to match request URI 'http://localhost:64789/api/Scores/GetScores/johnsmith"}
I have seen threads discussing problems with multiple Get methods, etc., but in this case, even with only one method enabled, I can't get the method with the string parameter to be recognised. Does anyone have any suggestions?
tonybater
0 Points
1 Post
WebApi Routing not working with a string parameter
Jan 09, 2013 10:40 AM|LINK
In a simple WebApi project, I want to make calls to either return an individual score by score id (int), or all scores for a particular patient (by patient name = string).
I have two routes configured:
api/Scores/GetScores/{pat_id} api/Scores/GetScore/{id}and my controller has two methods:
// GET api/Scores/GetScores/patientname [HttpGet] public IEnumerable<Score> GetScores(string pat_id) { return db.Scores.Where(model => model.PatientID==pat_id).AsEnumerable(); } //GET api/GetScores/GetScore/5 [HttpGet] public Score GetScore(int id) { Score score = db.Scores.Find(id); if (score == null) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound)); } return score; }If I have RouteDebugger enabled, then a URL of 'http://localhost:64789/api/Scores/GetScore/8' shows a match to the second route an ID of 8, and a URL of 'http://localhost:64789/api/Scores/GetScores/johnsmith' matches the first route with an id of johmsmith.
If I disable RouteDebugger, both URLs fails with an error like "No HTTP resource was found that matches the request URI 'http://localhost:64789/api/Scores/GetScore/8'.","MessageDetail":"No route providing a controller name was found to match request URI 'http://localhost:64789/api/Scores/GetScore/8'"}
I can comment out the GetScores(string pat_id) controller method, in which case the URL 'http://localhost:64789/api/Scores/GetScore/8' works, and returns the expected result. However, the reverse case, commenting out the GetScore(int id) method does NOT work, and the URL 'http://localhost:64789/api/Scores/GetScores/johnsmith still returns the same error - "No HTTP resource was found that matches the request URI 'http://localhost:64789/api/Scores/GetScores/johnsmith.","MessageDetail":"No route providing a controller name was found to match request URI 'http://localhost:64789/api/Scores/GetScores/johnsmith"}
I have seen threads discussing problems with multiple Get methods, etc., but in this case, even with only one method enabled, I can't get the method with the string parameter to be recognised. Does anyone have any suggestions?