0

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);
    }

1 Answer 1

2

You can use routing constraints:

try:

 [Route("api/Match/{id:guid}")]

and

 [Route("api/Match/{eventId:int}")]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.