Web Api OData v4 FromODataUri always returning 404 Not Found

2.2k views Asked by At

I' trying invoke a web api odata controller method that receives a parameter from the uri, like below:

    // GET /odata/People(3)
    public SingleResult<Person> Get([FromODataUri] int key)
    {
        return SingleResult.Create(DemoDataSources.Instance.People.Where(p => p.ID == key.ToString()).AsQueryable());
    }

The method above is not being hit by the url http://localhost:port/odata/People(3) always returning 404 Not Found.

I've configured a new Asp.Net OData Web Application from the scratch with the following files:

PeopleController.cs

[EnableQuery]
public class PeopleController : ODataController
{

    // GET /odata/People
    public IHttpActionResult Get()
    {
        return Ok(DemoDataSources.Instance.People.AsQueryable());
    }

    // GET /odata/People(3)
    public SingleResult<Person> Get([FromODataUri] int key)
    {
        return SingleResult.Create(DemoDataSources.Instance.People.Where(p => p.ID == key.ToString()).AsQueryable());
    }
}

WebApiConfig.cs

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services
        // Configure Web API to use only bearer token authentication.
        config.SuppressDefaultHostAuthentication();
        config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));

        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        config.MapODataServiceRoute("odata", "odata", GetEdmModel(), new DefaultODataBatchHandler(GlobalConfiguration.DefaultServer));
        config.EnsureInitialized();
    }

    private static IEdmModel GetEdmModel()
    {
        ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
        builder.Namespace = "Demos";
        builder.ContainerName = "DefaultContainer";
        builder.EntitySet<Person>("People");
        builder.EntitySet<Trip>("Trips");
        var edmModel = builder.GetEdmModel();
        return edmModel;
    }
}

DemoDataSources.cs

public class DemoDataSources
{
    private static DemoDataSources instance = null;
    public static DemoDataSources Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new DemoDataSources();
            }
            return instance;
        }
    }
    public List<Person> People { get; set; }
    public List<Trip> Trips { get; set; }
    private DemoDataSources()
    {
        this.Reset();
        this.Initialize();
    }
    public void Reset()
    {
        this.People = new List<Person>();
        this.Trips = new List<Trip>();
    }
    public void Initialize()
    {
        this.Trips.AddRange(new List<Trip>()
        {
            new Trip()
            {
                ID = "0",
                Name = "Trip 0"
            },
            new Trip()
            {
                ID = "1",
                Name = "Trip 1"
            },
            new Trip()
            {
                ID = "2",
                Name = "Trip 2"
            },
            new Trip()
            {
                ID = "3",
                Name = "Trip 3"
            }
        });
        this.People.AddRange(new List<Person>
        {
            new Person()
            {
                ID = "001",
                Name = "Angel",
                Trips = new List<Trip>{Trips[0], Trips[1]}
            },
            new Person()
            {
                ID = "002",
                Name = "Clyde",
                Description = "Contrary to popular belief, Lorem Ipsum is not simply random text.",
                Trips = new List<Trip>{Trips[2], Trips[3]}
            },
            new Person()
            {
                ID = "003",
                Name = "Elaine",
                Description = "It has roots in a piece of classical Latin literature from 45 BC, making Lorems over 2000 years old."
            }
        });
    }
}

Person.cs

public class Person
{
    [Key]
    public String ID { get; set; }
    [Required]
    public String Name { get; set; }
    public String Description { get; set; }
    public List<Trip> Trips { get; set; }
}

Trip.cs

public class Trip
{
    [Key]
    public String ID { get; set; }
    [Required]
    public String Name { get; set; }
}

I "think" this problem has to do with odata routing but I have no clue why such a basic behavior is not working properly ...

Appreciate any help! Marcos

2

There are 2 answers

0
Andriy Tolstoy On BEST ANSWER

You need to use single quotes for the id in the route because of the string type of the key property of the Person class: http://localhost:port/odata/People('3')

0
Thomas Van Herpe On

In the case you pass a string value as suggested by Andriy, you probably need to change the signature of the get as well.

So change: public SingleResult Get([FromODataUri] int key)

To: public SingleResult Get([FromODataUri] string key)

And then I think you can call the OData service as suggested by Andriy.