[英]Web Api 2.2 OData V4 Function Routing
I have a Web Api 2.2 project working with OData v4. 我有一个使用OData v4的Web Api 2.2项目。 The normal EntitySet configuration is working as desired with all http verbs.
正常的EntitySet配置可以根据需要使用所有http谓词。 Where I am having a problem is trying to expose a custom function.
我遇到问题的地方是尝试公开自定义函数。 I started off trying to do something different than the standard examples, but I have backed all the way up to just trying to getting a basic example function working.
我开始尝试做一些不同于标准示例的事情,但我一直支持尝试使基本示例函数正常工作。
Here is my startup config (straight from the MS examples): 这是我的启动配置(直接来自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<Product>("Products");
builder.Namespace = "ProductService";
builder.EntityType<Product>().Collection
.Function("MostExpensive")
.Returns<double>();
config.MapODataServiceRoute(
"odataroute"
, "odata"
, builder.GetEdmModel()
);
}
}
}
And here is my controller: 这是我的控制器:
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 IQueryable<Product> GetProducts()
{
return db.Products;
}
[HttpGet]
public IHttpActionResult MostExpensive()
{
double test = 10.3;
return Ok(test);
}
}
}
The regular GET, works fine: 常规GET,工作正常:
http://something/odata/Products
However, the following always gives me a 404: 但是,以下总是给我一个404:
http://something/odata/Products/ProductService.MostExpensive()
I have tried any number of different things with the namespace, etc... So, it doesn't work like all of the examples, but I'm at a loss at how to dig in deeper to figure out what is going wrong. 我已经使用命名空间等尝试了许多不同的东西......所以,它不像所有的例子那样工作,但是我不知道如何深入挖掘以找出问题所在。 The metadata exposed by
http://something/odata
doesn't provide any clues. http://something/odata
公开的元数据不提供任何线索。 Is there any other way to discover where (and how) this function should be exposed? 有没有其他方法可以发现这个函数应该暴露在哪里(以及如何)?
EDIT: Here is the link to the Microsoft Example I am following: http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/odata-actions-and-functions 编辑:以下是我所遵循的Microsoft示例的链接: http : //www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/odata-actions-和功能
Please change the element as below, which is the recommended way if there is dot in the request URL: 请更改下面的元素,如果请求URL中有点,这是推荐的方法:
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
and if 而如果
http://something/odata/Products/ProductService.MostExpensive()
is requested, I can get the data: 请求,我可以得到数据:
{
@odata.context: "http://localhost:14853/odata/$metadata#Edm.Double",
value: 3
}
I know this question is not recent, but I found another answer that works for me. 我知道这个问题不是最近的问题,但我找到了另一个对我有用的答案。 If you're willing to remove the namespace from the URL, you can use
如果您愿意从URL中删除命名空间,则可以使用
config.EnableUnqualifiedNameCall(true);
Your URL would then look like this: 您的网址将如下所示:
http://something/odata/Products/MostExpensive
See http://odata.github.io/WebApi/#06-01-custom-url-parsing . 见http://odata.github.io/WebApi/#06-01-custom-url-parsing 。 That's available in the Microsoft.AspNet.OData NuGet package.
这可以在Microsoft.AspNet.OData NuGet包中找到。
Then you may try adding the part not replacing them. 然后你可以尝试添加不替换它们的部分。 Mine looks like below and it can work.
我看起来像下面,它可以工作。
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
<clear/>
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="/*"
verb="*" type="System.Web.Handlers.TransferRequestHandler"
preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.