无法在ASP.NET Web API 2.2中启用OData v4.0路由

Chr*_*ars 5 c# odata asp.net-web-api

我试图在我的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();
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

有没有一个干净的方法来获得Microsoft.OData.Edm.IEdmModel?或者我将不得不做一个演员?

Chr*_*ars 9

用System.Web.OData.Builder替换System.Web.Http.OData.Builder似乎有效.

以下是与解释的链接:http://blogs.msdn.com/b/webdev/archive/2014/03/13/getting-started-with-asp-net-web-api-2-2-for-的OData-v4-0.aspx

我认为这条线几乎总结了它:

程序集名称和根命名空间现在是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;
Run Code Online (Sandbox Code Playgroud)