自定义 Odata v4 函数 - 不是有效的 odata 路由模板 - 找不到该段的资源
Custom Odata v4 function - is not a valid odata route template - resource not found for the segment
我有这样的想法(对自己的行动可能没问题)
[HttpPost]
[ODataRoute("GenerateFromProduct")]
public async Task<IHttpActionResult> GenerateFromProduct([FromBodyAttribute] Product product)
{
if(!ModelState.IsValid)
{
return BadRequest();
}
List<string[]> combos = new List<string[]>();
List<ProductVariant> productVariants = product.GenerateProductVariants();
db.ProductVariants.AddRange(productVariants);
await db.SaveChangesAsync();
return Ok(productVariants);
}
WebApiConfig 中定义的操作可能是这样的:
builder.EntityType<ProductVariant>().Collection
.Function("GenerateFromProduct").Returns<List<ProductVariant>>().EntityParameter<Product>("product");
但我不断收到以下错误(经过多次重写)
An exception of type 'System.InvalidOperationException' occurred in System.Web.OData.dll but was not handled in user code
Additional information: The path template 'GenerateFromProduct' on the action 'GenerateFromProduct' in controller 'ProductVariants' is not a valid OData path template. Resource not found for the segment
知道我做错了什么吗?我没有在网上找到很多关于 odata 和自定义函数/操作的信息,除了 msdn 上的那些。
@NicoJuicy
您的代码:
builder.EntityType<ProductVariant>().Collection.Function("GenerateFromProduct")...
就是定义一个Bound函数。绑定函数就像实例调用的 实例方法。
类似的,ODataRoute
中的Uri模板应该与绑定函数uri相同。所以,应该是:
[ODataRoute("ProductVariants/YourNameSpace.GenerateFromProduct(product={product}")]
此外,函数只在GET场景下使用,所以,[HttpPost]
函数不正确。
也许以下material可以帮助您理解OData中的function/action:
而下面的material可以帮助您理解function/action参数:
- http://odata.github.io/WebApi/#04-06-function-parameter-support
- http://odata.github.io/WebApi/#04-07-action-parameter-support
而下面的material可以帮助你理解属性路由:
我有这样的想法(对自己的行动可能没问题)
[HttpPost]
[ODataRoute("GenerateFromProduct")]
public async Task<IHttpActionResult> GenerateFromProduct([FromBodyAttribute] Product product)
{
if(!ModelState.IsValid)
{
return BadRequest();
}
List<string[]> combos = new List<string[]>();
List<ProductVariant> productVariants = product.GenerateProductVariants();
db.ProductVariants.AddRange(productVariants);
await db.SaveChangesAsync();
return Ok(productVariants);
}
WebApiConfig 中定义的操作可能是这样的:
builder.EntityType<ProductVariant>().Collection
.Function("GenerateFromProduct").Returns<List<ProductVariant>>().EntityParameter<Product>("product");
但我不断收到以下错误(经过多次重写)
An exception of type 'System.InvalidOperationException' occurred in System.Web.OData.dll but was not handled in user code
Additional information: The path template 'GenerateFromProduct' on the action 'GenerateFromProduct' in controller 'ProductVariants' is not a valid OData path template. Resource not found for the segment
知道我做错了什么吗?我没有在网上找到很多关于 odata 和自定义函数/操作的信息,除了 msdn 上的那些。
@NicoJuicy
您的代码:
builder.EntityType<ProductVariant>().Collection.Function("GenerateFromProduct")...
就是定义一个Bound函数。绑定函数就像实例调用的 实例方法。
类似的,ODataRoute
中的Uri模板应该与绑定函数uri相同。所以,应该是:
[ODataRoute("ProductVariants/YourNameSpace.GenerateFromProduct(product={product}")]
此外,函数只在GET场景下使用,所以,[HttpPost]
函数不正确。
也许以下material可以帮助您理解OData中的function/action:
而下面的material可以帮助您理解function/action参数:
- http://odata.github.io/WebApi/#04-06-function-parameter-support
- http://odata.github.io/WebApi/#04-07-action-parameter-support
而下面的material可以帮助你理解属性路由: