如何在OData v6.0.0中启用EnableCaseInsensitive,EnableEnumPrefixFree和EnableUnqualifiedNameCall [英] How to EnableCaseInsensitive, EnableEnumPrefixFree and EnableUnqualifiedNameCall in OData v6.0.0

查看:97
本文介绍了如何在OData v6.0.0中启用EnableCaseInsensitive,EnableEnumPrefixFree和EnableUnqualifiedNameCall的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近将OData从(v5.9.1)更新到了最新的稳定版本(v6.0.0),在以前的版本中,我曾经这样配置我的环境:

I recently updated OData from (v5.9.1) to the latest stable version (v6.0.0) and in the former one I used to configure my environment like this:

        //Allows calling the Url like {entityAction}/{id}
        config.SetUrlConventions(ODataUrlConventions.KeyAsSegment);

        //Allows urls to be case insensitive
        config.EnableCaseInsensitive(true);

        // Remove the necessity of having to specify the namespace of enums.
        config.EnableEnumPrefixFree(true);

        //This allows call a function without using the full namespace.
        config.EnableUnqualifiedNameCall(true);

        config.MapODataServiceRoute("odata", "api/rest",
        edmModel, new  DefaultODataBatchHandler(GlobalConfiguration.DefaultServer));

现在,更新后如何获得与以前相同的结果?我的所有路由均未运行,例如:"localhost/odata/people/"正在运行,但未显示以下消息:

Now after the update, how can I achieve the same result as before? None of my routes e.g: 'localhost/odata/people/' are working it shows the following message:

The path template 'people/{parentId}/emails' on the action 'Get' in controller 'PersonEmails' is not a valid OData path template. The operation import overloads matching 'people' are invalid. This is most likely an error in the IEdmModel.

有什么想法吗?预先感谢.

Any ideas? Thanks in advance.

推荐答案

我遇到了相同的问题. System.Web.OData中有一个内部类,称为UnqualifiedCallAndEnumPrefixFreeResolver.从理论上讲,这既可以处理EnumPrefixFree也可以处理UnqualifiedNameCall,但是由于它是内部的,因此我现在必须自己编写.

I encountered the same issue. There is an internal class in System.Web.OData called UnqualifiedCallAndEnumPrefixFreeResolver. This in theory would handle both EnumPrefixFree and UnqualifiedNameCall, but as this is internal i had to write my own for now.

public class UnqualifiedCallAndEnumPrefixFreeResolver : ODataUriResolver
{
    private readonly StringAsEnumResolver _stringAsEnum = new StringAsEnumResolver();
    private readonly UnqualifiedODataUriResolver _unqualified = new UnqualifiedODataUriResolver();

    private bool _enableCaseInsensitive;

    public override bool EnableCaseInsensitive
    {
        get { return this._enableCaseInsensitive; }
        set
        {
            this._enableCaseInsensitive = value;
            _stringAsEnum.EnableCaseInsensitive = this._enableCaseInsensitive;
            _unqualified.EnableCaseInsensitive = this._enableCaseInsensitive;
        }
    }

    #region UnqualifiedODataUriResolver

    public override IEnumerable<IEdmOperation> ResolveBoundOperations(IEdmModel model, string identifier,
        IEdmType bindingType)
    {
        return _unqualified.ResolveBoundOperations(model, identifier, bindingType);
    }

    public override IEnumerable<IEdmOperation> ResolveUnboundOperations(IEdmModel model, string identifier)
    {
        return _unqualified.ResolveUnboundOperations(model, identifier);
    }

    #endregion

    #region StringAsEnumResolver

    public override void PromoteBinaryOperandTypes(BinaryOperatorKind binaryOperatorKind,
        ref SingleValueNode leftNode, ref SingleValueNode rightNode, out IEdmTypeReference typeReference)
    {
        _stringAsEnum.PromoteBinaryOperandTypes(binaryOperatorKind, ref leftNode, ref rightNode, out typeReference);
    }

    public override IEnumerable<KeyValuePair<string, object>> ResolveKeys(IEdmEntityType type,
        IDictionary<string, string> namedValues, Func<IEdmTypeReference, string, object> convertFunc)
    {
        return _stringAsEnum.ResolveKeys(type, namedValues, convertFunc);
    }

    public override IEnumerable<KeyValuePair<string, object>> ResolveKeys(IEdmEntityType type,
        IList<string> positionalValues, Func<IEdmTypeReference, string, object> convertFunc)
    {
        return _stringAsEnum.ResolveKeys(type, positionalValues, convertFunc);
    }

    public override IDictionary<IEdmOperationParameter, SingleValueNode> ResolveOperationParameters(
        IEdmOperation operation, IDictionary<string, SingleValueNode> input)
    {
        return _stringAsEnum.ResolveOperationParameters(operation, input);
    }

    #endregion
}

用法如下:

 configuration.MapODataServiceRoute(
            "ODataRoute",
            null,
            builder =>
                builder.AddService(ServiceLifetime.Singleton, sp => BuildModel())
                    .AddService<IEnumerable<IODataRoutingConvention>>(ServiceLifetime.Singleton, sp =>
                            ODataRoutingConventions.CreateDefaultWithAttributeRouting("ODataRoute", configuration))
                    .AddService<ODataUriResolver>(ServiceLifetime.Singleton, sp => new UnqualifiedCallAndEnumPrefixFreeResolver
                    {
                        EnableCaseInsensitive = true
                    })
        );          

我也将此问题发布在GitHub上,但是到目前为止,团队没有任何答案,在我们获得标准配置之前,此工作环境是一种替代选择.

I also posted this on GitHub as an issue, but for now no answer from the team, this workarround is an alternative until we get something in standard.

Github链接

关于, 密海

这篇关于如何在OData v6.0.0中启用EnableCaseInsensitive,EnableEnumPrefixFree和EnableUnqualifiedNameCall的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆