网页API 2.2的OData V4路由功能 [英] Web Api 2.2 OData V4 Function Routing

查看:273
本文介绍了网页API 2.2的OData V4路由功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web API 2.2项目与OData的V4的工作。正常EntitySet的配置是否与所有的HTTP动词期望。在那里我有一个问题,就是试图揭露一个自定义函数。我开始尝试做一些比标准的例子不同,但我已备份所有到只是想获得一个基本的例子功能的工作方式。

I have a Web Api 2.2 project working with OData v4. The normal EntitySet configuration is working as desired with all http verbs. 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.

下面是我的启动配置(直接从MS的例子):

Here is my startup config (straight from the MS examples):

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);
        }
    }
}

常规GET,正常工作

The regular GET, works fine:

http://something/odata/Products

但是,下面总是给我一个404:

However, the following always gives me a 404:

http://something/odata/Products/ProductService.MostExpensive()

我曾尝试与命名空间,等等......所以,它并不像所有的例子工作任何数量的不同的东西,但我在研究如何在更深的挖掘亏损弄清楚什么是脚麻。通过 HTTP暴露元数据://东西/ ODATA 并没有提供任何线索。是否有任何其他的方式来发现其中(以及如何)这个函数应该被曝光?

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. Is there any other way to discover where (and how) this function should be exposed?

编辑:这里是链接到我下面微软示例:
<一href=\"http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/odata-actions-and-functions\">http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/odata-actions-and-functions

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

推荐答案

请更改元素如下,这是推荐的方式,如果有圆点请求网址:

Please change the element as below, which is the recommended way if there is dot in the request URL:

 <system.webServer>
        <modules runAllManagedModulesForAllRequests="true" />
 </system.webServer>

如果

http://something/odata/Products/ProductService.MostExpensive()

要求,我可以得到的数据:

is requested, I can get the data:

{
@odata.context: "http://localhost:14853/odata/$metadata#Edm.Double",
value: 3
}

这篇关于网页API 2.2的OData V4路由功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆