I am trying to create two get methods with different parameters using Route. In my database I have a table with two columns: EventId and CategoryId.
I want to return the list of all matches in one case where the EventId is used in the url, for example: api/Match/5, and in another case where the CategoryId is used in the url, for example: api/Match/425D750E-56BD-412C-8A48-38C2FBE5B24C.
In my MatchController setup shown below the method that has the EventId works fine but the one with the Guid CategoryId returns a bad request (400) and the following error message:
The parameters dictionary contains a null entry for parameter 'eventId' of non-nullable type 'System.Int32' for method 'System.Web.Http.IHttpActionResult GetMatchRecord(Int32)' in 'Nybroe.FightPlan.Web.Api.MatchController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
It looks like it is using the same methods for each situation. Even though that I specify it with the Route[].
Am I doing something wrong with the Route[] setup? I have also tried to completely remove the Route[] at each method but the outcome is still the same.
MatchController:
// GET api/Match/CategoryId
[Route("api/Match/{CategoryId}")]
[ResponseType(typeof(MatchRecord))]
public IHttpActionResult GetMatchRecord(Guid id)
{
var matches = db.Matches.Where(record => record.CategoryId == id).ToList();
if (matches == null)
{
return NotFound();
}
return Ok(matches);
}
// GET api/Match/EventId
[Route("api/Match/{EventId}")]
[ResponseType(typeof(MatchRecord))]
public IHttpActionResult GetMatchRecord(int eventId)
{
var matches = db.Matches.Where(record => record.EventId == eventId).ToList();
if (matches == null)
{
return NotFound();
}
return Ok(matches);
}