簡體   English   中英

在OData中對參數使用未綁定操作

[英]Using an Unbound Action with Parameters in the OData

使用未綁定的操作和參數解析OData URI時,將引發以下異常: The request URI is not valid. The segment 'TestAction' cannot include key predicates, however it may end with empty parenthesis The request URI is not valid. The segment 'TestAction' cannot include key predicates, however it may end with empty parenthesis 下面的代碼重現異常:

        var builder = new ODataConventionModelBuilder
        {
            Namespace = "Test",
            ContainerName = "Test"
        };

        var action = builder.Action("TestAction").Returns<long>();

        action.Parameter<int?>("x");
        action.Parameter<int?>("y");

        var model = builder.GetEdmModel();

        var parser = new ODataUriParser(model, new Uri("TestAction(x=1,y=2)", UriKind.Relative));

        var path = parser.ParsePath();

我研究了Microsoft.OData.Core源代碼,並在FunctionOverloadResolver類中找到了它:

        try
        {
            if (parameterNames.Count > 0)
            {
                // In this case we have to return a function so filter out actions because the number of parameters > 0.
                candidateMatchingOperationImports = resolver.ResolveOperationImports(model, identifier).RemoveActionImports(out foundActionImportsWhenLookingForFunctions).FilterFunctionsByParameterNames(parameterNames, resolver.EnableCaseInsensitive).Cast<IEdmOperationImport>().ToList();
            }
            else
            {
                candidateMatchingOperationImports = resolver.ResolveOperationImports(model, identifier).ToList();
            }
        }
        catch (Exception exc)
        {
            if (!ExceptionUtils.IsCatchableExceptionType(exc))
            {
                throw;
            }

            throw new ODataException(ODataErrorStrings.FunctionOverloadResolver_FoundInvalidOperationImport(identifier), exc);
        }

        if (foundActionImportsWhenLookingForFunctions.Count > 0)
        {
            throw ExceptionUtil.CreateBadRequestError(ODataErrorStrings.RequestUriProcessor_SegmentDoesNotSupportKeyPredicates(identifier));
        }

當操作具有參數時,為什么解析程序類需要過濾掉操作?

我終於解決了問題。 動作參數應位於請求有效負載上。 因此,URI應該如下所示:

    var parser = new ODataUriParser(model, new Uri("TestAction", UriKind.Relative));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM