odata v4 Product({key})/GenerateVariants 路由配置错误
odata v4 Product({key})/GenerateVariants route misconfiguration
产品上有一个函数可以将变体生成为列表。目前 returns:
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()"
}
}
}
}
我有点想知道应该调整什么才能使这项工作正常进行。
出于某种原因(我想我都试过了),以下更改在 productVariantsController 中是正确的(最初它在 ProductsController 中)
我最后的改动是:
builder.EntityType<ProductVariant>()
.Function("GenerateFromProduct")
.ReturnsCollectionFromEntitySet<ProductVariant>("ProductVariants");
包括迁移路由(在 webapiconfig 中)和 ProductVariantsController。
产品上有一个函数可以将变体生成为列表。目前 returns:
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()"
}
}
}
}
我有点想知道应该调整什么才能使这项工作正常进行。
出于某种原因(我想我都试过了),以下更改在 productVariantsController 中是正确的(最初它在 ProductsController 中)
我最后的改动是:
builder.EntityType<ProductVariant>()
.Function("GenerateFromProduct")
.ReturnsCollectionFromEntitySet<ProductVariant>("ProductVariants");
包括迁移路由(在 webapiconfig 中)和 ProductVariantsController。