使用 2 个参数的 ode vc

odata v4 using 2 parameter

我只想将 2 个参数传递给控制器​​和 运行 我的 sql-函数 尝试用谷歌搜索很多资源,但没有成功,

谁能给我一些提示?

基本上我遵循这个Web API and OData- Pass Multiple Parameters

当我使用 builder.Function 编译器时,一直告诉我找不到扩展方法。

package.config

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="EntityFramework" version="6.1.3" targetFramework="net452" />
  <package id="EntityFramework.Functions" version="1.4.0" targetFramework="net452" />
  <package id="Microsoft.AspNet.OData" version="6.0.0" targetFramework="net452" />
  <package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.WebApi.OData" version="5.3.1" targetFramework="net452" />
  <package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net452" />
  <package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="1.0.0" targetFramework="net452" />
  <package id="Microsoft.Data.Edm" version="5.6.0" targetFramework="net452" />
  <package id="Microsoft.Data.OData" version="5.6.0" targetFramework="net452" />
  <package id="Microsoft.Extensions.DependencyInjection" version="1.0.0" targetFramework="net452" />
  <package id="Microsoft.Extensions.DependencyInjection.Abstractions" version="1.0.0" targetFramework="net452" />
  <package id="Microsoft.Net.Compilers" version="1.0.0" targetFramework="net452" developmentDependency="true" />
  <package id="Microsoft.OData.Core" version="7.0.0" targetFramework="net452" />
  <package id="Microsoft.OData.Edm" version="7.0.0" targetFramework="net452" />
  <package id="Microsoft.Spatial" version="7.0.0" targetFramework="net452" />
  <package id="Newtonsoft.Json" version="6.0.4" targetFramework="net452" />
  <package id="System.Spatial" version="5.6.0" targetFramework="net452" />
</packages>

我的WebApiConfog.cs

using Microsoft.OData.Edm;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System.Web.Http;
using System.Web.Http.OData.Builder;
using System.Web.Http.OData.Extensions;
using wcod;
using wcod.Model;
namespace wcod
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services
            ODataModelBuilder builder = new ODataConventionModelBuilder();

            //  config.Count().Filter().OrderBy().Expand().Select().MaxTop(null);

            var json = config.Formatters.JsonFormatter;
            json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
            config.Formatters.Remove(config.Formatters.XmlFormatter);



            config.Formatters.Remove(config.Formatters.XmlFormatter);
            config.Formatters.JsonFormatter.SerializerSettings.Formatting = Formatting.Indented;
            config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
            // var json = config.Formatters.JsonFormatter;
            // builder.EntitySet<Booking>("Bookings");
            builder.EntitySet<LiveBooking>("LiveBookings");
            builder.EntitySet<TimeMarker>("TimeMarkers");
            builder.EntitySet<BookingInfo>("BookingInfoes");
            builder.EntitySet<LiveBookingByType>("LiveBookingByTypes");






            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
            config.Routes.MapODataServiceRoute("odata", "odata/v4", builder.GetEdmModel());
            // config.MapODataServiceRoute( routeName: "ODataRoute", routePrefix: "odata/v4", model: builder.GetEdmModel());
        }

    }
}

好吧,我遵循这种方法:

WebApiConfig.cs

using System.Web.Http;
using System.Web.OData.Extensions;
using System.Web.OData.Builder;
//using System.Web.OData.Extensions;
using wcod.Model;

namespace wcod
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services
            ODataModelBuilder builder = new ODataConventionModelBuilder();
            config.Count().Filter().OrderBy().Expand().Select().MaxTop(null);

            builder.EntitySet<TimeMarker>("TimeMarkers");

            var function = builder.Function("GetTimeMarkerSearch");
            function.Parameter<string>("bookID");
            function.Parameter<string>("keywords");
            function.ReturnsCollectionFromEntitySet<TimeMarker>("TimeMarkers");
                config.MapHttpAttributeRoutes();

            //config.Routes.MapODataServiceRoute("odata", "odata/v4", builder.GetEdmModel());

            config.MapODataServiceRoute("odata", "odata/v4", builder.GetEdmModel());
        }

    }
}

然后在我的控制器中,mycase TimeMarkersControllers

[HttpGet]
[ODataRoute("GetTimeMarkerSearch(bookID={bookID},keywords={keywords})")]
public IHttpActionResult WhateverName([FromODataUri]string bookID, [FromODataUri]string keywords){
// my logic
}

您应该使用 System.Web.OData.Builder 而不是 System.Web.Http.OData.Builder。它应该在包 Microsoft.AspNet.OData.

如果 .GetEdModel() 抛出异常,则可能是您的定义有问题,而不是使用问题。