简体   繁体   中英

$expand in OData V4

* Update: Was able to follow this tutorial , and now the error I'm getting states:

"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'."

Any ideas?*

I am getting an error when trying to retrieve the Color in my Pet query using OData V4. I'm having quite a bit of trouble, ideally I would use an expand on color (eg localhost:0000/api/v1/Pets('dog')?$expand=Colors)

The JSON I need returned is something like:

[
  {
    "_Key": "1",
    "animalname": "dog",
    "furcolorname": "black,white",
    "color": {
      "_Key": "1",
      "colorname": "black"
    },
    {
      "_Key": "2",
      "colorname": "white"
    }
  }
]

Maybe I'm on the completely wrong path, either way any input is appreciated!

If I query localhost:0000/api/v1/Pets('dog') :

…
"Message\": \"Invalid column name 'Pet__Key'.\"     
…

If I query localhost:0000/api/v1/Pets('dog')?$exand=Colors :

…
"The query specified in the URI is not valid. Could not find a property named 'Colors' on type 'PetShop.Odata.Models.Pet'."    
…

Pet.cs model:

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 model:

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:

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:

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:

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

ColorConfig.cs:

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

MyContext.cs:

…
public DbSet<Pet> Pets { get; set; }
public DbSet<Color> Colors { get; set; }
…

Setup.cs:

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

Assuming you are using default routing convention, your Get colors method in PetsController.cs does not match the expected OData route format.

Instead of the following:

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

You should try:

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

This definition would make the OData route: http://localhost:0000/api/v1/Pets('dog')/Colors

For more information on routing conventions see https://docs.microsoft.com/en-us/odata/webapi/built-in-routing-conventions .

A couple alternative approaches exist as well:

  1. Register a different name to a custom nav property
  2. Define a custom OData Function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM