绑定函数返回404

时间:2014-11-26 06:54:40

标签: c# asp.net .net asp.net-web-api odata

我正在尝试将绑定函数添加到我的Web API 2.2 OData v4控制器中,但它返回404并显示错误消息:

{
  "error":{
    "code":"","message":"No HTTP resource was found that matches the request URI 'http://localhost:2390/Hierarchies('300924834')/FullHierarchy'.","innererror":{
      "message":"No routing convention was found to select an action for the OData path with template '~/entityset/key/unresolved'.","type":"","stacktrace":""
    }
  }
}

这是HierarchiesController上的方法:

[HttpGet]
public IHttpActionResult FullHierarchy([FromODataUri]string key)
{
     return Ok(new Hierarchy());
}

我尝试在我的方法上添加属性[ODataRoute("Hierarchies({key}/FullHierarchy)")],但在第一次运行应用程序时,我在第GlobalConfiguration.Configure(WebApiConfig.Register);行上遇到以下错误:

  

控制器“Hierarchies”中操作“FullHierarchy”上的路径模板“Hierarchies({key} / FullHierarchy)”不是有效的OData路径模板。错误请求 - 查询语法错误。

这是我在WebApiConfig中的配置:

ODataConventionModelBuilder builder = new ODataConventionModelBuilder();

builder.Namespace = ""; // tried with and without this line.

var hierachyType = builder.EntitySet<Hierarchy>("Hierarchies").EntityType;
hierachyType.Function("FullHierarchy").Returns<Hierarchy>();

请注意,对集合的绑定函数也不起作用,但未绑定的函数可以正常工作。

这就是$metadata的样子:

<?xml version="1.0" encoding="UTF-8"?>
<edmx:Edmx xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx" Version="4.0">
    <edmx:DataServices>
        <Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="Entities.Models">
            <EntityType Name="Hierarchy">
                <Key>
                    <PropertyRef Name="Id" />
                </Key>
                <Property Name="Name" Type="Edm.String" />
                <Property Name="Id" Type="Edm.String" Nullable="false" />
                <NavigationProperty Name="Policy" Type="Entities.Models.Policy" />
                <NavigationProperty Name="Nodes" Type="Collection(Entities.Models.HierarchyNode)" />
            </EntityType>
            <EntityType Name="Node" OpenType="true">
                <Key>
                    <PropertyRef Name="Id" />
                </Key>
                <Property Name="Name" Type="Edm.String" />
                <Property Name="Id" Type="Edm.String" Nullable="false" />
            </EntityType>
            <EntityType Name="Site" BaseType="Entities.Models.Node" OpenType="true">
                <Property Name="Address" Type="Edm.String" />
            </EntityType>
            <EntityType Name="Policy">
                <Key>
                    <PropertyRef Name="Id" />
                </Key>
                <Property Name="Id" Type="Edm.String" Nullable="false" />
                <Property Name="Name" Type="Edm.String" />
                <NavigationProperty Name="Rules" Type="Collection(Entities.Rules.Rule)" />
            </EntityType>
            <EntityType Name="HierarchyNode">
                <Key>
                    <PropertyRef Name="Id" />
                </Key>
                <Property Name="Id" Type="Edm.String" Nullable="false" />
                <Property Name="Name" Type="Edm.String" />
                <NavigationProperty Name="Nodes" Type="Collection(Entities.Models.HierarchyNode)" />
            </EntityType>
            <EntityType Name="Building" BaseType="Entities.Models.Node" OpenType="true">
                <Property Name="StaffCount" Type="Edm.Int32" Nullable="false" />
            </EntityType>
            <EntityType Name="Source" BaseType="Entities.Models.Node" OpenType="true">
                <Property Name="GroupByOperation" Type="Edm.String" />
                <Property Name="Factor" Type="Edm.Int32" Nullable="false" />
            </EntityType>
        </Schema>
        <Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="Entities.Rules">
            <EntityType Name="Rule" Abstract="true">
                <Key>
                    <PropertyRef Name="Id" />
                </Key>
                <Property Name="Id" Type="Edm.String" Nullable="false" />
                <Property Name="Name" Type="Edm.String" />
                <Property Name="NodeType" Type="Edm.String" />
            </EntityType>
            <EntityType Name="ParentChildRule" BaseType="Entities.Rules.Rule">
                <Property Name="AllowedParentsTypes" Type="Edm.String" />
                <Property Name="ForbiddenParentsTypes" Type="Edm.String" />
            </EntityType>
            <EntityType Name="PredicateRule_1OfTNode" BaseType="Entities.Rules.Rule">
                <Property Name="Predicate" Type="System.Linq.Expressions.Expression_1OfFunc_2OfTNode_String" />
            </EntityType>
            <EntityType Name="PropertyRule" BaseType="Entities.Rules.Rule">
                <Property Name="RequiredProperties" Type="Edm.String" />
            </EntityType>
        </Schema>
        <Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="System.Linq.Expressions">
            <ComplexType Name="Expression_1OfFunc_2OfTNode_String">
                <Property Name="Parameters" Type="Collection(System.Linq.Expressions.ParameterExpression)" />
            </ComplexType>
            <ComplexType Name="ParameterExpression" />
        </Schema>
        <Schema xmlns="http://docs.oasis-open.org/odata/ns/edm">
            <Function Name="FullHierarchy" IsBound="true">
                <Parameter Name="bindingParameter" Type="Entities.Models.Hierarchy" />
                <ReturnType Type="Entities.Models.Hierarchy" />
            </Function>
            <EntityContainer Name="Container">
                <EntitySet Name="Hierarchies" EntityType="Entities.Models.Hierarchy" />
                <EntitySet Name="Nodes" EntityType="Entities.Models.Node" />
                <EntitySet Name="Sites" EntityType="Entities.Models.Site" />
                <EntitySet Name="Rules" EntityType="Entities.Rules.Rule" />
            </EntityContainer>
        </Schema>
    </edmx:DataServices>
</edmx:Edmx>

1 个答案:

答案 0 :(得分:2)

有几件事:

  1. 架构需要具有命名空间。即builder.Namespace = "";需要删除或替换为非空字符串。
  2. 该功能需要由其完全限定名称调用。即请求URL必须为http://localhost:2390/Hierarchies('300924834')/<namespace>.FullHierarchy,其中<namespace>应为默认命名空间或您在第1项中指定的名称空间。
  3. 作为绑定到单个实体的函数,编写这样的控制器方法就足够了: [HttpGet] public IHttpActionResult FullHierarchy() { return Ok(new Hierarchy()); } 我不知道具体原因。我猜这个常规模型构建器可以帮助你进行这样的路由。
  4. 它对我有用,以下是我的测试功能细节:

    WebApiConfig:

    builder.EntityType<Product>().Function("SomeFunction").Returns<string>();
    

    在ProductsController中:

    [HttpGet]
        public IHttpActionResult SomeFunction()
        {
            return Ok("Some");
        }
    

    请求:

    GET http://localhost:54017/Products(1)/Default.SomeFunction