odata v4 Product({key})/ GenerateVariants路由配置错误

时间:2015-10-14 23:10:16

标签: asp.net asp.net-mvc odata asp.net-web-api2 odata-v4

产品上有一个函数可以将变体生成为列表。目前它返回:

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

这是我的WebApiConfig:

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

这是我在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);
    }

我实际上希望我的productController生成ProductVariants列表。但我目前的错误是:

{
  "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()"
      }
    }
  }
}

我有点想知道应该调整什么来使这项工作。

1 个答案:

答案 0 :(得分:0)

出于某种原因(我以为我全部尝试过),以下更改在productVariantsController中是正确的(最初是在ProductsController中)

我最后的改变是:

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

包括迁移路线(在webapiconfig中)和ProductVariantsController。