我有一个使用OData v4的Web Api 2.2项目.正常的EntitySet配置可以根据需要使用所有http谓词.我遇到问题的地方是尝试公开自定义函数.我开始尝试做一些不同于标准示例的事情,但我一直支持尝试使基本示例函数正常工作.
这是我的启动配置(直接来自MS示例):
using System; using System.Collections.Generic; using System.Linq; using System.Web.Http; using System.Web.OData.Builder; using System.Web.OData.Extensions; namespace Test.Service { public static class WebApiConfig { public static void Register(HttpConfiguration config) { // other entitysets that don't have functions builder.EntitySet("Products"); builder.Namespace = "ProductService"; builder.EntityType ().Collection .Function("MostExpensive") .Returns (); config.MapODataServiceRoute( "odataroute" , "odata" , builder.GetEdmModel() ); } } }
这是我的控制器:
using System.Data.Entity; using System.Data.Entity.Infrastructure; using System.Linq; using System.Net; using System.Threading.Tasks; using System.Web.Http; using System.Web.OData; namespace Test.Service.Controllers { public class ProductsController : ODataController { private EntityContext db = new EntityContext(); [EnableQuery] public IQueryableGetProducts() { return db.Products; } [HttpGet] public IHttpActionResult MostExpensive() { double test = 10.3; return Ok(test); } } }
常规GET,工作正常:
http://something/odata/Products
但是,以下总是给我一个404:
http://something/odata/Products/ProductService.MostExpensive()
我已经使用命名空间等尝试了许多不同的东西......所以,它不像所有的例子那样工作,但是我不知道如何深入挖掘以找出问题所在.公开的元数据http://something/odata
不提供任何线索.有没有其他方法可以发现这个函数应该暴露在哪里(以及如何)?
编辑:以下是我所遵循的Microsoft示例的链接:http: //www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/odata-actions-和功能
我知道这个问题不是最近的问题,但我找到了另一个对我有用的答案.如果您愿意从URL中删除命名空间,则可以使用
config.EnableUnqualifiedNameCall(true);
您的网址将如下所示:
http://something/odata/Products/MostExpensive
见http://odata.github.io/WebApi/#06-01-custom-url-parsing.这可以在Microsoft.AspNet.OData NuGet包中找到.
请更改下面的元素,如果请求URL中有点,这是推荐的方法:
<system.webServer> <modules runAllManagedModulesForAllRequests="true" /> </system.webServer>
而如果
http://something/odata/Products/ProductService.MostExpensive()
请求,我可以得到数据:
{ @odata.context: "http://localhost:14853/odata/$metadata#Edm.Double", value: 3 }