Code Monkey home page Code Monkey logo

Comments (7)

julealgon avatar julealgon commented on June 29, 2024 1

Thanks @a-ney. I just wanted to make sure this was independent of [EnableQuery] itself. Hopefully the OData team will have more information to share with you about this.

In the meantime, you might want to rename your issue to remove the coupling with [EnableQuery] since it happens regardless of it.

from aspnetcoreodata.

julealgon avatar julealgon commented on June 29, 2024

Stacktrace say's it's because of EnableQuery attribute on my GET action which uses it's own FilterBinder instead of CustomFilterBinder.

Does it work fine if you remove the [EnableQuery] attribute and manually handle a ODataQueryOptions<T> instead?

from aspnetcoreodata.

a-ney avatar a-ney commented on June 29, 2024

@julealgon No, exception is the same.

Microsoft.OData.ODataException: An unknown function with name 'custom' was found. This may also be a function import or a key lookup on a navigation property, which is not allowed.
 at Microsoft.OData.UriParser.FunctionCallBinder.GetUriFunctionSignatures(String functionCallToken, Boolean enableCaseInsensitive)
 at Microsoft.OData.UriParser.FunctionCallBinder.BindAsUriFunction(FunctionCallToken functionCallToken, List`1 argumentNodes)
 at Microsoft.OData.UriParser.FunctionCallBinder.BindFunctionCall(FunctionCallToken functionCallToken)
 at Microsoft.OData.UriParser.MetadataBinder.BindFunctionCall(FunctionCallToken functionCallToken)
 at Microsoft.OData.UriParser.MetadataBinder.Bind(QueryToken token)
 at Microsoft.OData.UriParser.BinaryOperatorBinder.GetOperandFromToken(BinaryOperatorKind operatorKind, QueryToken queryToken)
 at Microsoft.OData.UriParser.BinaryOperatorBinder.BindBinaryOperator(BinaryOperatorToken binaryOperatorToken)
 at Microsoft.OData.UriParser.MetadataBinder.BindBinaryOperator(BinaryOperatorToken binaryOperatorToken)
 at Microsoft.OData.UriParser.MetadataBinder.Bind(QueryToken token)
 at Microsoft.OData.UriParser.FilterBinder.BindFilter(QueryToken filter)
 at Microsoft.OData.UriParser.ODataQueryOptionParser.ParseFilterImplementation(String filter, ODataUriParserConfiguration configuration, ODataPathInfo odataPathInfo)
 at Microsoft.OData.UriParser.ODataQueryOptionParser.ParseFilter()
 at Microsoft.AspNetCore.OData.Query.FilterQueryOption.get_FilterClause()
 at Microsoft.AspNetCore.OData.Query.FilterQueryOption.ApplyTo(IQueryable query, ODataQuerySettings querySettings)
 at Microsoft.AspNetCore.OData.Query.ODataQueryOptions.ApplyTo(IQueryable query, ODataQuerySettings querySettings)
 at Microsoft.AspNetCore.OData.Query.ODataQueryOptions`1.ApplyTo(IQueryable query, ODataQuerySettings querySettings)
 at Microsoft.AspNetCore.OData.Query.ODataQueryOptions.ApplyTo(IQueryable query)
 at Microsoft.AspNetCore.OData.Query.ODataQueryOptions`1.ApplyTo(IQueryable query)\r\n
 at ... //my action call below

from aspnetcoreodata.

xuzhg avatar xuzhg commented on June 29, 2024

@a-ney the methods (for example, geo.distance) listed in the https://devblogs.microsoft.com/odata/customizing-filter-for-spatial-data-in-asp-net-core-odata-8/ are built-in methods, it means that's supported by default in the OData library, but there's no default binding in the ASP.NET Core side . So, The post is trying to bind the methods to a particular C# method.

Your scenario is different, You want to 'define' a cusomized method that is not defined in the OData. You should use the "customized Uri function" feature. It's listed at https://learn.microsoft.com/en-us/odata/webapi/bind-custom-urifunctions-to-clr-methods.

Here's a version from my side:

public static void AddCustomFunc()
{
    FunctionSignatureWithReturnType customStringEdmFunction =
             new FunctionSignatureWithReturnType(
            EdmCoreModel.Instance.GetInt32(true),
            EdmCoreModel.Instance.GetString(true));

    MethodInfo customStringMethodInfo = typeof(Extensions).GetMethod("MyCustomMethod", new Type[] { typeof(string) });
    const string customMethodName = "custom";
    ODataUriFunctions.AddCustomUriFunction(customMethodName, customStringEdmFunction, customStringMethodInfo);
}

public static int MyCustomMethod(string input)
{
    // you should replace the codes using your logic
    switch (input)
    {
        case "foo": return 1;
        case "bar": return 2;
        case "tik": return 3;
        case "tok": return 4;
        default: return 0;
    }
}

Then, call 'AddCustomFunc' some place in 'Program.cs' before call using middleware. Then it should work (you don't need to customize the IFilterBinder);

(Known issue: AddCustomUriFunction uses the 'static' to hold the function mapping, it's not good design and better to refactor it in the ODL 8).

from aspnetcoreodata.

a-ney avatar a-ney commented on June 29, 2024

@xuzhg wow, thanks a lot, this is exactly what I needed!

from aspnetcoreodata.

xuzhg avatar xuzhg commented on June 29, 2024

reopen for more discussion about the refactor

from aspnetcoreodata.

rbalagangadharan avatar rbalagangadharan commented on June 29, 2024

@a-ney Can you provide working sample of this?

from aspnetcoreodata.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.