Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get ASP.Net Web API and OData to bind a string value as a key?

I'm going through a short Web Api + OData tutorial from asp.net: http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/getting-started-with-odata-in-web-api/create-a-read-only-odata-endpoint.

I downloaded the example project, and it works. But then I started playing around with the Product model that they use in the example. I added a new property to act as a key of type string instead of an integer key.

The new Product.cs:

public class Product
{
    public string stringKey { get; set; }
    public int ID { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
    public string Category { get; set; }
}

The modified controller:

public class ProductsController : EntitySetController<Product, string>
{
    static List<Product> products = new List<Product>()
    {
        new Product() { stringKey = "one", ID = 1, Name = "Hat", Price = 15, Category = "Apparel" },
        new Product() { stringKey = "two", ID = 2, Name = "Socks", Price = 5, Category = "Apparel" },
        new Product() { stringKey = "three", ID = 3, Name = "Scarf", Price = 12, Category = "Apparel" },
        new Product() { stringKey = "four", ID = 4, Name = "Yo-yo", Price = 4.95M, Category = "Toys" },
        new Product() { stringKey = "five", ID = 5, Name = "Puzzle", Price = 8, Category = "Toys" },
    };

    [Queryable]
    public override IQueryable<Product> Get()
    {
        return products.AsQueryable();
    }

    protected override Product GetEntityByKey(string key)
    {
        return products.FirstOrDefault(p => p.stringKey == key);
    }
}

The trouble is that when I go to /odata/Products(one) the string "one" is not bound to the key argument in the GetEntityByKey(string key) action. However, when I browse to odata/Products(1) then "1" does get bound to the key argument.

How can I get a string with text values to bind correctly, instead of just binding strings with numerical values?

Update

I forgot to include the WebApiConfig:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        ODataModelBuilder modelBuilder = new ODataConventionModelBuilder();
        modelBuilder.EntitySet<Product>("Products");

        Microsoft.Data.Edm.IEdmModel model = modelBuilder.GetEdmModel();
        config.Routes.MapODataRoute("ODataRoute", "odata", model);
    }
}
like image 398
quakkels Avatar asked Jun 10 '13 14:06

quakkels


1 Answers

I noticed that the path /odata/Products(0011-1100) would only bind "0011" as the string key.

After some playing around with it, I've found that the following path works as I had hoped:

/odata/Products('one')

It appears the single quotes are required to read the entire string within the parentheses.

like image 183
quakkels Avatar answered Oct 09 '22 13:10

quakkels