WebApi OData HttpConfiguration扩展方法缺失

时间:2016-11-15 16:50:58

标签: .net asp.net-web-api2 odata

OData v4 HttpConfiguration扩展方法对我来说似乎不见了。这是我的Web API注册方法:

    public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API routes
        config.MapHttpAttributeRoutes();

        // Use camel case for JSON data.
        config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();

        config.EnableCaseInsensitive();

        config.Count().Filter().OrderBy().Expand().Select().MaxTop(null);            
        config.MapODataServiceRoute("odata", "api", OdataConfig.GetEdm());

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

    }
}

System.Web.Odata(.Extensions)中完全没有像EnableCaseInsensitive()EnableEnumPrefixFree()这样的方法。

它们出现在documentationsource code的装配中,但在探索我已经得到的装配时,它们很容易丢失:

missing extensions screenshot

我尝试过多次从Nuget重新安装OData - 我似乎找不到其他任何人遇到此问题。关于准备拔出我的头发。有任何想法吗?

2 个答案:

答案 0 :(得分:2)

我认为降级不是一个好的解决方案:D

API已更改为依赖注入:http://odata.github.io/WebApi/#13-04-DependencyInjection

您必须执行以下操作: (实施例)

public static void Register(HttpConfiguration config)
{
    // OData
    config.Count().Filter().OrderBy().Expand().Select().MaxTop(null);

    config.MapODataServiceRoute("odata", "odata", BuildRoute);
}

private static void BuildRoute(IContainerBuilder builder)
{
    builder
        .AddService(ServiceLifetime.Singleton, s => GetEdmModel())
        .AddService<IEnumerable<IODataRoutingConvention>>(ServiceLifetime.Singleton, s => ODataRoutingConventions.CreateDefault())
        .AddService<ODataUriResolver>(ServiceLifetime.Singleton, s => new CaseInsensitiveODataUriResolver());
}

My Resolver类如下所示:

public class CaseInsensitiveODataUriResolver : UnqualifiedODataUriResolver
{
    public override bool EnableCaseInsensitive
    {
        get
        {
            return true;
        }

        set
        {
        }
    }
}

您可以从ODataUriResolverUnqualifiedODataUriResolverStringAsEnumResolver派生。

不可能写出像

这样的东西
s => new ODataUriResolver { EnableCaseInsensitive = true }

请参阅 https://github.com/OData/WebApi/issues/812https://github.com/OData/WebApi/issues/853

答案 1 :(得分:1)

Aaaaand在我发布之后,我明白了。通过将Nuget软件包降级到5.9.1,我最终得到了丢失的部分。