Odata 使用lowerCamelCase查询Web API 2.2

Odata 使用lowerCamelCase查询Web API 2.2,odata,asp.net-web-api,asp.net-web-api2,Odata,Asp.net Web Api,Asp.net Web Api2,我将Web API 2.2与[EnableQuery]一起使用,如下所示: public class ProductsController : ApiController { private MyContext db = new MyContext(); [EnableQuery] public IQueryable<Product> GetProducts() { return db.Products; } } 因此,我

我将Web API 2.2与
[EnableQuery]
一起使用,如下所示:

public class ProductsController : ApiController
{
    private MyContext db = new MyContext();


    [EnableQuery]
    public IQueryable<Product> GetProducts()
    {
        return db.Products;
    }
}

因此,我想知道ApiController是否可以做到这一点?

您需要执行以下步骤:

  • 定义自定义EnableQueryAttribute:

    public class MyEnableQueryAttribute:EnableQueryAttribute
    {
        public override IEdmModel GetModel(Type elementClrType, HttpRequestMessage request,
            HttpActionDescriptor actionDescriptor)
        {
            // Get model for the request
            IEdmModel model = request.ODataProperties().Model;
    
            if (model == null)
            {
                // user has not configured anything or has registered a model without the element type
                // let's create one just for this type and cache it in the action descriptor
                model = actionDescriptor.Properties.GetOrAdd("System.Web.OData.Model+" + elementClrType.FullName, _ =>
                {
                    ODataConventionModelBuilder builder =
                        new ODataConventionModelBuilder(actionDescriptor.Configuration, isQueryCompositionMode: true);
                    builder.EnableLowerCamelCase();
                    EntityTypeConfiguration entityTypeConfiguration = builder.AddEntityType(elementClrType);
                    builder.AddEntitySet(elementClrType.Name, entityTypeConfiguration);
                    IEdmModel edmModel = builder.GetEdmModel();
                    Contract.Assert(edmModel != null);
                    return edmModel;
                }) as IEdmModel;
            }
    
            Contract.Assert(model != null);
            return model;
        }
    }
    
  • 将其添加到控制器中的操作:

    public class ProductsController : ApiController
    {
        [MyEnableQuery]
        public IHttpActionResult Get()
        {
            IList<Product> products=new List<Product>();
            products.Add(new Product() { Id = 1, Name = "Name1",Category=new Category(){Id=1,Name="Category1" }});
            products.Add(new Product() { Id = 2, Name = "Name2", Category = new Category() { Id = 2, Name = "Category2" } });
    
    
            return Ok(products.AsQueryable<Product>());
        }
    }
    
    我把整个解决方案放在这里:,仅供参考

    public class ProductsController : ApiController
    {
        [MyEnableQuery]
        public IHttpActionResult Get()
        {
            IList<Product> products=new List<Product>();
            products.Add(new Product() { Id = 1, Name = "Name1",Category=new Category(){Id=1,Name="Category1" }});
            products.Add(new Product() { Id = 2, Name = "Name2", Category = new Category() { Id = 2, Name = "Category2" } });
    
    
            return Ok(products.AsQueryable<Product>());
        }
    }
    
    GET http://localhost:12568/api/Products?$expand=category