Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在ASP.NET Web API 2.2中启用OData v4.0路由时出现问题_C#_Asp.net Web Api_Odata - Fatal编程技术网

C# 在ASP.NET Web API 2.2中启用OData v4.0路由时出现问题

C# 在ASP.NET Web API 2.2中启用OData v4.0路由时出现问题,c#,asp.net-web-api,odata,C#,Asp.net Web Api,Odata,我正在尝试在我的ASP.NET web api中实现OData路由。为了获得指导,我看了本教程: 但是,我在MapODataServiceRoute()函数中不断收到错误消息。显然,该函数需要一个Microsoft.OData.Edm.IEdmModel,而我的生成器的GetEdmModel()函数只返回Microsoft.Data.Edm.IEdmModel 我在网上做了一些调查。Microsoft.Data.Edm是旧版本OData的库。Microsoft.OData.Edm适用于OData

我正在尝试在我的ASP.NET web api中实现OData路由。为了获得指导,我看了本教程:

但是,我在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();
        }

    }
}

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

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

以下是说明的链接:

我认为这句话大致概括了这一点:

程序集名称和根命名空间现在是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;
我希望这会有所帮助, 当我尝试执行相同的示例时,我遇到了相同的错误,我只是通过PackageManager控制台执行了以下操作

1-卸载软件包Microsoft.OData.Edm-Force 2-安装软件包Microsoft.OData.Edm

那我就可以用了。

请使用

using Microsoft.AspNet.OData.Builder;
using Microsoft.AspNet.OData.Extensions;
而不是

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