【问题标题】:Quering Web API 2.2 with lowerCamelCase使用 lowerCamelCase 查询 Web API 2.2
【发布时间】:2014-09-11 22:39:14
【问题描述】:

我正在使用带有 [EnableQuery] 的 Web API 2.2,如下所示:

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


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

一旦我使用CamelCasePropertyNamesContractResolver,我想执行这样的OData 查询:api/products/?$expand=categories 而不是api/products/?$expand=Categories

我使用ODataController 测试了 OData v4(我不想使用它,因为 DateTime 属性)并且它工作正常:

ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EnableLowerCamelCase(); 

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

【问题讨论】:

    标签: odata asp.net-web-api asp.net-web-api2


    【解决方案1】:

    您需要执行以下步骤:

    1. 定义一个自定义的 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;
          }
      }
      
    2. 将其添加到控制器中的操作中:

      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
    

    我已将整个解决方案放在这里:https://github.com/tanjinfu/WebApiODataSamples/tree/master/EnableCamelCaseForApiController,仅供参考。

    【讨论】:

      猜你喜欢
      • 2018-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多