How to EnableCaseInsensitive, EnableEnumPrefixFree and EnableUnqualifiedNameCall in OData v6.0.0

不想你离开。 提交于 2019-12-03 16:10:31

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
}

The usage would be as follows:

 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
                    })
        );          

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 link

Regards, Mihai

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!