Web Api 2.2 OData V4 函数路由 [英] Web Api 2.2 OData V4 Function Routing

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

问题描述

我有一个使用 OData v4 的 Web Api 2.2 项目.正常的 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()                        
                );
        }
    }
}

这是我的控制器:

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://something/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?

这是我正在关注的 Microsoft 示例的链接: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

推荐答案

请更改如下元素,如果请求 URL 中存在点,建议采用以下方式:

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
}

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

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