odata v4 Product({key})/GenerateVariants route misconfiguration

℡╲_俬逩灬. 提交于 2019-12-13 04:44:14

问题


There is a function on product that generates the variants as a list. Currently it returns:

The related entity set could not be found from the OData path

This is my WebApiConfig:

builder.EntityType<Product>().Function("GenerateVariants").Returns<List<ProductVariant>>();
////.Parameter<string>("save").OptionalParameter = true;

This is my method on the ProductController

    //http://localhost:26696/odata/Products(b2a35842-7b68-e511-beda-6c71d92133bc)/GenerateVariants
    [HttpGet]
    [ODataRoute("Products({key})/GenerateVariants")]
    public async Task<IHttpActionResult> GenerateVariants([FromODataUri] Guid key) // bool save = false
    {
        var product = await db.Products.Include("option1").Include("option2").Include("option3").Where(el => el.Id == key).FirstOrDefaultAsync();
        List<ProductVariant> productVariants = product.GenerateProductVariants();
        //if (save)
        //{
        //    db.ProductVariants.AddRange(productVariants);
        //    await db.SaveChangesAsync();
        //}
        return Ok(productVariants);
    }

I actually want my productController to generate a list of ProductVariants. But my current error is this:

{
  "error":{
    "code":"","message":"An error has occurred.","innererror":{
      "message":"The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; odata.metadata=minimal'.","type":"System.InvalidOperationException","stacktrace":"","internalexception":{
    "message":"The related entity set could not be found from the OData path. The related entity set is required to serialize the payload.","type":"System.Runtime.Serialization.SerializationException","stacktrace":"   bij System.Web.OData.Formatter.Serialization.ODataFeedSerializer.WriteObject(Object graph, Type type, ODataMessageWriter messageWriter, ODataSerializerContext writeContext)\r\n   bij System.Web.OData.Formatter.ODataMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, HttpContent content, HttpContentHeaders contentHeaders)\r\n   bij System.Web.OData.Formatter.ODataMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken)\r\n--- Einde van stacktracering vanaf vorige locatie waar uitzondering is opgetreden ---\r\n   bij System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   bij System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   bij System.Runtime.CompilerServices.TaskAwaiter.GetResult()\r\n   bij System.Web.Http.WebHost.HttpControllerHandler.<WriteBufferedResponseContentAsync>d__1b.MoveNext()"
      }
    }
  }
}

I'm kinda wondering what should be adjusted to make this work.


回答1:


For some reason ( i thought i tried them all), the following change was correct in productVariantsController ( originally it was in the ProductsController)

My final change was :

            builder.EntityType<ProductVariant>()
            .Function("GenerateFromProduct")
            .ReturnsCollectionFromEntitySet<ProductVariant>("ProductVariants");

Including migrating the routes ( in webapiconfig) and on ProductVariantsController.



来源:https://stackoverflow.com/questions/33137197/odata-v4-productkey-generatevariants-route-misconfiguration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!