如何获得ASP.Net的Web API和OData的绑定一个字符串值作为密钥? [英] How to get ASP.Net Web API and OData to bind a string value as a key?

查看:111
本文介绍了如何获得ASP.Net的Web API和OData的绑定一个字符串值作为密钥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经历短暂的Web API +的OData从asp.net教程:<一href=\"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\" rel=\"nofollow\">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.

我下载了示例项目,和它的作品。但后来我开始与产品模式,他们在本例中使用的玩耍。我添加了一个新的属性作为字符串类型而不是整数键的键。

新Product.cs:

 公共类产品
{
    公共字符串stringKey {搞定;组; }
    公众诠释ID {搞定;组; }
    公共字符串名称{;组; }
    公共十进制价格{搞定;组; }
    公共字符串类别{搞定;组; }
}

修改后的控制器:

 公共ProductsController类:EntitySetController&LT;产品,串&GT;
{
    静态列表&LT;产品与GT;产品=新的List&LT;产品及GT;()
    {
        新产品(){stringKey =一,ID = 1,名称=帽子,价格= 15,类别=服饰},
        新产品(){stringKey =两节,ID = 2,名称=袜,价格= 5,类别=服饰},
        新产品(){stringKey =三,ID = 3,名称=围巾,价格= 12,类别=服饰},
        新产品(){stringKey =四个一,ID = 4,名称=溜溜球,价格= 4.95M,类别=玩具},
        新产品(){stringKey =十二五,ID = 5,名称=谜,​​价格= 8,类别=玩具},
    };    [可查询]
    公共重写IQueryable的&LT;产品与GT;得到()
    {
        返回products.AsQueryable();
    }    保护覆盖产品GetEntityByKey(字符串键)
    {
        返回products.FirstOrDefault(P =&GT; p.stringKey ==密钥);
    }
}

麻烦的是,当我去 / ODATA /产品(一个)字符串一是不是在 GetEntityByKey(字符串键)的行动。然而,当我浏览到 ODATA /产品(1)那么1被打绑定到参数。

我怎样才能使用文本值的字符串以正确绑定,而不是仅仅与数值结合的字符串,?

更新

我忘了包括WebApiConfig:

 公共静态类WebApiConfig
{
    公共静态无效的注册(HttpConfiguration配置)
    {
        ODataModelBuilder模型构建器=新ODataConventionModelBuilder();
        modelBuilder.EntitySet&LT;产品与GT(产品);        Microsoft.Data.Edm.IEdmModel模型= modelBuilder.GetEdmModel();
        config.Routes.MapODataRoute(ODataRoute,ODATA模型);
    }
}


解决方案

我注意到,路径/ ODATA /产品(0011-1100)将只能绑定0011的字符串键。

在一些与它玩耍,我发现以下路径工程,我所希望的:

  / ODATA /产品(一)

它出现的单引号都必须阅读括号内的整个字符串。

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

解决方案

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.

这篇关于如何获得ASP.Net的Web API和OData的绑定一个字符串值作为密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆