首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >$expand in OData V4

$expand in OData V4
EN

Stack Overflow用户
提问于 2020-09-23 15:34:30
回答 1查看 504关注 0票数 0

*更新:能够遵循本教程,现在我得到的错误是:

代码语言:javascript
复制
"message": "No HTTP resource was found that matches the request URI 'http://localhost:0000/api/v1/Pets('dog')/Color'."
      "message": "No routing convention was found to select an action for the OData path with template '~/entityset/key/unresolved'."

有什么想法吗?*

当试图使用OData V4检索宠物查询中的颜色时,我收到了一个错误。我遇到了很多麻烦,理想情况下,我应该在颜色上使用扩展(例如,localhost:0000/api/v1/Pets('dog')?$expand=Colors) )

我需要返回的JSON类似于:

代码语言:javascript
复制
[
  {
    "_Key": "1",
    "animalname": "dog",
    "furcolorname": "black,white",
    "color": {
      "_Key": "1",
      "colorname": "black"
    },
    {
      "_Key": "2",
      "colorname": "white"
    }
  }
]

也许我是在完全错误的道路上,任何一种方式的任何输入都是感激的!

如果我查询localhost:0000/api/v1/Pets(“狗”):

代码语言:javascript
复制
…
"Message\": \"Invalid column name 'Pet__Key'.\"     
…

如果我查询localhost:0000/api/v1/Pets('dog')?$exand=Colors:

代码语言:javascript
复制
…
"The query specified in the URI is not valid. Could not find a property named 'Colors' on type 'PetShop.Odata.Models.Pet'."    
…

Pet.cs模型:

代码语言:javascript
复制
namespace PetShop.Odata.Models
{
   [Table("pet")]
   public class Pet
   {
      [Key]
      public string _Key { get; set; }
      public string AnimalName { get; set; }

      [ForeignKey("Color")]
      public string FurColorName { get; set; }
      public virtual Color Color { get; set; }

      public virtual ICollection<Color> Colors { get; set; }
   }
}

Color.cs模型:

代码语言:javascript
复制
namespace PetShop.Odata.Models
{
   [Table("color")]
   public class Color
   {
      public Color()
      {
         new HashSet<Pet>();
      }

      [Key]
      public string _Key { get; set; }
      public string ColorName { get; set; }
   }
}

PetsController.cs:

代码语言:javascript
复制
namespace PetShop.Odata.Controllers
{
   [ApiVersion("1.0")]
   [ODataRoutePrefix("Pets")]
   [ApiControllerMetricReport]
   public class PetsController : ODataController
   {
      private readonly MyContext context = new MyContext ();

      [HttpGet]
      [ODataRoute]
      [EnableQuery]
      [ResponseType(typeof(List<Pet>))]
      public IHttpActionResult Get()
      {
         return Ok(context.Pets.AsQueryable());
      }

      [EnableQuery]
      public IQueryable<Color> Get ([FromODataUri] string key)
      {
         return context.Pets.Where(m => m._Key == key).SelectMany(a => a.Colors);
      }

      protected override void Dispose(bool disposing)
      {
         context.Dispose();
         base.Dispose(disposing);
      }
   }
}

ColorsController.cs:

代码语言:javascript
复制
namespace PetShop.Odata.Controllers
{
   [ApiVersion("1.0")]
   [ODataRoutePrefix("Colors")]
   [ApiControllerMetricReport]
   public class ColorsController : ODataController
   {
      private readonly MyContext context = new MyContext ();

      [HttpGet]
      [ODataRoute]
      [EnableQuery]
      [ResponseType(typeof(List<Color>))]
      public IHttpActionResult Get()
      {
         return Ok(context.Colors.AsQueryable());
      }

      protected override void Dispose(bool disposing)
      {
         context.Dispose();
         base.Dispose(disposing);
      }
   }
}

PetConfig.cs:

代码语言:javascript
复制
namespace PetShop.Odata.Configuration
{   public class PetModelConfiguration : IModelConfiguration
   {
      public void Apply(ODataModelBuilder builder, ApiVersion apiVersion)
      {
         builder.EntitySet<Pet>("Pets");
      }
   }
}

ColorConfig.cs:

代码语言:javascript
复制
namespace PetShop.Odata.Configuration
{   public class ColorModelConfiguration : IModelConfiguration
   {
      public void Apply(ODataModelBuilder builder, ApiVersion apiVersion)
      {
         builder.EntitySet<Color>("Colors");
      }
   }
}

MyContext.cs:

代码语言:javascript
复制
…
public DbSet<Pet> Pets { get; set; }
public DbSet<Color> Colors { get; set; }
…

Setup.cs:

代码语言:javascript
复制
…
public static HttpServer CreateHttpServer()
      {
         var httpConfig = new HttpConfiguration();
         var webApiServer = new HttpServer(httpConfig);

         httpConfig.AddApiVersioning(options => options.ReportApiVersions = true);

         httpConfig.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();

         var modelBuilder = new VersionedODataModelBuilder(httpConfig)
         {
            ModelBuilderFactory = () => new ODataConventionModelBuilder().EnableLowerCamelCase(),

            ModelConfigurations =
          {
              new PetConfig(),
              new ColorConfig()
            },
         };
         var models = modelBuilder.GetEdmModels();

         httpConfig.Count().Filter().OrderBy().Expand().Select().MaxTop(null);
…
EN

回答 1

Stack Overflow用户

发布于 2020-10-14 05:29:25

假设您使用的是默认路由约定,则PetsController.cs中的Get颜色方法与预期的OData路由格式不匹配。

而不是采取以下措施:

代码语言:javascript
复制
[EnableQuery]
public IQueryable<Color> Get ([FromODataUri] string key)
{
   return context.Pets.Where(m => m._Key == key).SelectMany(a => a.Colors);
}

你应该试着:

代码语言:javascript
复制
[EnableQuery]
public IQueryable<Color> GetColors ([FromODataUri] string key)
{
   return context.Pets.Where(m => m._Key == key).SelectMany(a => a.Colors);
}

这个定义将使OData路由:http://localhost:0000/api/v1/Pets('dog')/Colors

有关路由约定的更多信息,请参见https://learn.microsoft.com/en-us/odata/webapi/built-in-routing-conventions

还有几种替代办法:

  1. 将不同的名称注册到自定义nav属性
  2. 定义自定义OData函数
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64031440

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档