0

我只想将 2 个参数传递给控制器​​并运行我的 sql 函数尝试并在没有运气的情况下搜索了很多资源,

谁能给我一些提示?

基本上我遵循这个Web API 和 OData-传递多个参数

当我使用builder.Function编译器时,请告诉我没有找到扩展方法。

包配置

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

    }
}
4

2 回答 2

1

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

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

于 2017-07-26T06:35:09.947 回答
0

Well i follow this approach:

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

    }
}

then in my Controller, mycase TimeMarkersControllers

[HttpGet]
[ODataRoute("GetTimeMarkerSearch(bookID={bookID},keywords={keywords})")]
public IHttpActionResult WhateverName([FromODataUri]string bookID, [FromODataUri]string keywords){
// my logic
}
于 2017-07-26T03:11:05.883 回答