Web Api 2.2 OData V4功能路由

 ZZ张朝_288 发布于 2022-12-28 11:52

我有一个使用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 IQueryable GetProducts()
        {
            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-和功能

2 个回答
  • 我知道这个问题不是最近的问题,但我找到了另一个对我有用的答案.如果您愿意从URL中删除命名空间,则可以使用

    config.EnableUnqualifiedNameCall(true);
    

    您的网址将如下所示:

    http://something/odata/Products/MostExpensive
    

    见http://odata.github.io/WebApi/#06-01-custom-url-parsing.这可以在Microsoft.AspNet.OData NuGet包中找到.

    2022-12-28 11:53 回答
  • 请更改下面的元素,如果请求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
    }
    

    2022-12-28 11:55 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有