0

I have created a simple ApiController whit 3 functions.

  • get() // Get all
  • get(int id) // Get by id
  • getFirst() // get the firsts in the list

I have 2 scenarios and I'll appreciate a lot a proper explanation.

  • 1: If I implement only the get() and get(int id) it's working perfect when I call to api/controller/{id} (id optional)

  • 2: When I have the 3 functions when I try to access to api/controller/function/{id} (id optional) it's not working. Only works the function to get by id. you can see the error I'm getting below.

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

Api Class:

public class PersonController : ApiController { private Person[] listPerson;

    public PersonController() {
        listPerson = new Person[3];
        listPerson[0] = new Person { 
            id = 1,
            firstName = "test",
            lastName = "test",
            email = "test@gmail.com",
            age = 26
        };
        listPerson[1] = new Person
        {
            id = 2,
            firstName = "test",
            lastName = "test",
            email = "test@gmail.com",
            age = 26
        };
        listPerson[2] = new Person
        {
            id = 3,
            firstName = "test",
            lastName = "test",
            email = "test@gmail.com",
            age = 26
        };
    }

    public Person GetById(int id) {
        try
        {
            var person = this.listPerson.Where(p => p.id == id).FirstOrDefault();
            if (person != null)
            {
                return person;
            }
            else {
                throw new InvalidOperationException();
            }
        }
        catch (Exception) {
            throw new HttpResponseException(HttpStatusCode.NotFound);
        }
    }

    public Person[] Get()
    {
        try
        {
            if (this.listPerson != null)
            {
                return this.listPerson;
            }
            else
            {
                throw new InvalidOperationException();
            }
        }
        catch (Exception)
        {
            throw new HttpResponseException(HttpStatusCode.NotFound);
        }
    }

    public Person GetFirst()
    {
        try
        {
            if (this.listPerson != null)
            {
                return this.listPerson.Where(x=>x.Equals(1)).FirstOrDefault();
            }
            else
            {
                throw new InvalidOperationException();
            }
        }
        catch (Exception)
        {
            throw new HttpResponseException(HttpStatusCode.NotFound);
        }
    }
}

WebApiConfig:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}

It's like the ApiController only recognize the get and the get?id, so someone can explain me why and how to solve this issue?

Thanks

gon250
  • 3,405
  • 6
  • 44
  • 75

1 Answers1

1

Custom method naming is asp.net web api.

By default the route configurations follows RESTful Conventions of GET,POST,PUT,Delete.

Custom method names in ASP.NET Web API

go through this post it will solve your problem.

Should work using the code below, just adding {action}

routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{action}/{id}",
    defaults: new { action = "get", id = RouteParameter.Optional }
);
Community
  • 1
  • 1
Prashant
  • 460
  • 3
  • 12