【问题标题】:Trouble enabling OData v4.0 routing in ASP.NET Web API 2.2在 ASP.NET Web API 2.2 中启用 OData v4.0 路由时出现问题
【发布时间】:2015-05-29 16:05:20
【问题描述】:

我正在尝试在我的 ASP.NET Web api 中实现 OData 路由。为了获得指导,我查看了本教程:http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/create-an-odata-v4-endpoint

但是,我不断在 MapODataServiceRoute() 函数中收到错误消息。显然,该函数需要 Microsoft.OData.Edm.IEdmModel,而我的构建器的 GetEdmModel() 函数仅返回 Microsoft.Data.Edm.IEdmModel。

我在网上做了一些研究。 Microsoft.Data.Edm 是旧版 OData 的库。 Microsoft.OData.Edm 适用于 OData v4.0,这就是我在 WebApiConfig.cs 文件中注释掉 Microsoft.Data.Edm 的原因。这是我的代码。

using MyApp.Models;
// using Microsoft.Data.Edm;
using Microsoft.OData.Edm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Web.Http;
using System.Web.Http.OData.Builder;
using System.Web.OData.Extensions;
using System.Web.OData.Routing;

namespace MyAppAPI
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Enable attribute routing
            config.MapHttpAttributeRoutes();

            // Enable OData routing
            config.MapODataServiceRoute(
                routeName: "MyApp",
                routePrefix: "odata",
                model: GetEdmModel());

            // Conventional routing
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            // Uncomment the following line of code to enable query support for actions with an IQueryable or IQueryable<T> return type.
            // To avoid processing unexpected or malicious queries, use the validation settings on QueryableAttribute to validate incoming queries.
            // For more information, visit http://go.microsoft.com/fwlink/?LinkId=279712.
            config.EnableQuerySupport();

            // To disable tracing in your application, please comment out or remove the following line of code
            // For more information, refer to: http://www.asp.net/web-api
            config.EnableSystemDiagnosticsTracing();

            // Trying to get most browsers (i.e. Google Chrome) to return JSON
            config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
        }

        // Configure modesl to use Odata
        public static IEdmModel GetEdmModel()
        {
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            builder.EntitySet<ModelA>("ModelA");
            builder.EntitySet<ModelB>("ModelB");
            return builder.GetEdmModel();
        }

    }
}

但是,我仍然收到一条错误消息:

Error   1   Cannot implicitly convert type 'Microsoft.Data.Edm.IEdmModel' to 'Microsoft.OData.Edm.IEdmModel'. An explicit conversion exists (are you missing a cast?)

有没有获得 Microsoft.OData.Edm.IEdmModel 的干净方法?还是我只需要做一个演员表?

【问题讨论】:

    标签: c# asp.net-web-api odata


    【解决方案1】:

    用 System.Web.OData.Builder 替换 System.Web.Http.OData.Builder 似乎可行。

    这里是解释的链接:https://devblogs.microsoft.com/aspnet/getting-started-with-asp-net-web-api-2-2-for-odata-v4-0/

    不过,我认为这句话概括了它:

    程序集名称和根命名空间现在是 System.Web.OData 而不是 System.Web.Http.OData。

    我现在使用的标题是:

    using MyApp.Models;
    // using Microsoft.Data.Edm;
    using Microsoft.OData.Edm;
    using System.Net.Http.Headers;
    using System.Web.Http;
    // using System.Web.Http.OData.Builder;
    using System.Web.OData.Builder;
    using System.Web.OData.Extensions;
    

    【讨论】:

      【解决方案2】:

      请使用

      using Microsoft.AspNet.OData.Builder;
      using Microsoft.AspNet.OData.Extensions;
      

      而不是

      using System.Web.OData.Builder;
      using System.Web.OData.Extensions;
      

      【讨论】:

        【解决方案3】:

        我希望这会有所帮助, 当我尝试做同样的例子时,我得到了同样的错误,我只是通过包管理器控制台做了以下操作

        1- unInstall-Package Microsoft.OData.Edm -Force 2- 安装包 Microsoft.OData.Edm

        那么它对我来说很好。

        【讨论】:

          猜你喜欢
          • 2014-10-08
          • 1970-01-01
          • 2015-01-24
          • 1970-01-01
          • 1970-01-01
          • 2015-09-08
          • 1970-01-01
          • 2020-04-18
          • 1970-01-01
          相关资源
          最近更新 更多