在获取和QUOT相关实体ASP.NET的WebAPI的OData v4的结果;没有HTTP资源发现,请求URI&QUOT匹配; [英] Getting related entities ASP.NET WebApi OData v4 results in "No HTTP resource was found that matches the request URI"

查看:331
本文介绍了在获取和QUOT相关实体ASP.NET的WebAPI的OData v4的结果;没有HTTP资源发现,请求URI&QUOT匹配;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我也跟着<一个href=\"http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/entity-relations-in-odata-v4\"相对=nofollow>由麦克·沃森这asp.net教程,并设法建立相关实体就好了,但是当我申请这个逻辑,我的项目比较复杂的实体关系(因为有更多的其中,这是唯一的区别)在电话OData的不会成功,我得到了404这个有效载荷:

I followed this asp.net tutorial by Mike Wasson, and managed to set up the related entities just fine, but when I applied this logic to my project the more complex entity relations (in that there are more of them; that's the only difference) wouldn't succeed in an OData call, I got a 404 with this payload:

{
  "error": {
    "code": "",
    "message": "No HTTP resource was found that matches the request URI 'http://localhost:19215/Menus(c94f7f98-6987-e411-8119-984be10349a2)/MenuPermissions'.",
    "innererror": {
      "message": "No routing convention was found to select an action for the OData path with template '~/entityset/key/unresolved'.",
      "type": "",
      "stacktrace": ""
    }
  }
}

本教程没有提到不必设置EdmModel导航和迈克·沃森使得指出asp.net是官方文档:-)点;所以,我花了一段时间试图让这些相关实体的工作,以为我已经建立了这个项目不正确。

The tutorial doesn't mention having to set up EdmModel navigations and Mike Wasson makes a point of pointing out that "asp.net is official documentation :-)"; so, I have spent a while trying to get these related entities working, thinking I had set up the project incorrectly.

我想它可能有一些做与被的NuGet安装ASP.NET的OData库的版本(的NuGet控制台安装6.9.x,而对话的NuGet安装6.5.x中)。我也想知道是不是因为我设置的项目成一个完全空的项目,然后用OWIN,所以我采用了纯ASP.NET模板解决方案尝试过。我还尝试了其他几个可能的解决方案:在我的控制器方法的OData路由的属性;而包括我的数据层和模型都在同一个库(我把它们分离出来,以保持干燥);我甚至试图使用<一个href=\"http://blogs.msdn.com/b/webdev/archive/2013/04/04/debugging-asp-net-web-api-with-route-debugger.aspx\"相对=nofollow>里克·安德森的路线的WebAPI调试 - !我不会试图再次使用该

I thought it might have something to do with the version of ASP.NET OData libraries that NuGet was installing (the NuGet Console installs 6.9.x, whereas the NuGet Dialog installs 6.5.x). I also wondered if it was because I set the project up as a completely empty project and then use OWIN, so I tried it with a pure ASP.NET templated solution. I also tried a couple of other possible solutions: OData-route-attributes on my controller methods; and including my data layer and models all in the same library (I have them separated out to keep DRY); I even attempted to use the WebApi route debugger by Rick Anderson - I wouldn't attempt to use this again!

一切都无济于事。

有一个短暂的时刻,他们的工作,但我不知道为什么;他们不再对下构建/运行工作 - 我想我在两者之间改变的东西,但它是非常轻微的,我的每一步失去信心。

There was a brief moment when they worked, but I don't know why; they ceased to work on the next build/run - I guess I changed something in between, but it was very minor and I was losing confidence at every step.

于是我决定,迈克·沃森一定是刚刚阻力最小的路径在他的教程,所以我恢复到<一个href=\"http://stackoverflow.com/questions/23082927/adding-a-custom-query-backed-navigation-property-to-odataconventionmodelbuilder#23113443\">this SO问题/答案并修改了它的使用与ODataConventionModelBuilder和再利用,我将在我的回答如下解释。

Then I decided that Mike Wasson must've just taken the path of least resistance in his tutorial and so I reverted to this SO question/answer and modified it for use with ODataConventionModelBuilder and reuse, as I will explain in my answer below.

如果任何人得到这个工作的一个简单的方法知道,请告诉我,否则我建议刚咬咬牙,在我的回答如下写那些EdmModel-导航。

If anyone knows of a simpler way of getting this to work, please let me know, otherwise I recommend just biting the bullet and writing those EdmModel-Navigations in my answer below.

推荐答案

正如我在这个问题提了,我尝试了许多解决方案,以得到这个工作,但没有在实际上解决问题是一致的,我一直避免奠定了解决方案出<一个href=\"http://stackoverflow.com/questions/23082927/adding-a-custom-query-backed-navigation-property-to-odataconventionmodelbuilder#23113443\">this SO问题/答案因为教程是专门为V4和我想通这个问题的答案必须是一个旧版本(如何不明智的)。

As I mention in the question, I tried many solutions to get this to work, but none were consistent in actually solving the problem and I kept avoiding the solution laid out in this SO question/answer because the tutorial is specifically for v4 and I figured that answer must be for an older version (how unwise).

这样的回答也解决不了问题,但需要一些工作直接插入的OData v4和一个ODataConventionModelBuilder;这就是为什么我张贴了这个问题和答案;提供了解决方案,专门针对的OData v4和ODataConventionModelBuilder,希望别人不会失去我已经寻找到这个时间。

So that answer does solve the problem, but requires some work to fit directly into OData v4 and an ODataConventionModelBuilder; this is why I have posted this question and answer; to provide a solution, specifically for OData v4 and ODataConventionModelBuilder, in the hope that others won't lose the time I have looking into this.

首先,设置您的EdmModel:

First, set up your EdmModel:

private static IEdmModel GetEdmModel()
{
    var builder = new ODataConventionModelBuilder();
    builder.EnableLowerCamelCase();
    builder.EntitySet<Menu>("Menus");
    builder.EntitySet<MenuPermission>("MenuPermissions");
    var edmModel = builder.GetEdmModel();
    AddNavigations(edmModel); //see below for this method
    return edmModel;
}

二AddNavigations:

Second AddNavigations:

private static void AddNavigations(IEdmModel edmModel)
{
    AddMenuPermissionsNavigation(edmModel);
}

private static void AddMenuPermissionsNavigation(IEdmModel edmModel)
{
    var menus = (EdmEntitySet) edmModel.EntityContainer.FindEntitySet("Menus");
    var menuPermissions = (EdmEntitySet)edmModel.EntityContainer.FindEntitySet("MenuPermissions");
    var menuType = (EdmEntityType) edmModel.FindDeclaredType("iiid8.cms.data.models.Menu"); //"iiid8.cms.data.models" is the C# namespace
    var menuPermissionType = (EdmEntityType)edmModel.FindDeclaredType("iiid8.cms.data.models.MenuPermission"); //as above, "iiid8.cms.data.models" is the C# namespace
    AddOneToManyNavigation("MenuPermissions", menus, menuPermissions, menuType, menuPermissionType);
    AddManyToOneNavigation("Menu", menus, menuPermissions, menuType, menuPermissionType);
}

private static void AddOneToManyNavigation(string navTargetName, EdmEntitySet oneEntitySet, EdmEntitySet manyEntitySet,
    EdmEntityType oneEntityType, EdmEntityType manyEntityType)
{
    var navPropertyInfo = new EdmNavigationPropertyInfo
    {
        TargetMultiplicity = EdmMultiplicity.Many,
        Target = manyEntityType,
        ContainsTarget = false,
        OnDelete = EdmOnDeleteAction.None,
        Name = navTargetName
    };
    oneEntitySet.AddNavigationTarget(oneEntityType.AddUnidirectionalNavigation(navPropertyInfo), manyEntitySet);
}

private static void AddManyToOneNavigation(string navTargetName, EdmEntitySet oneEntitySet, EdmEntitySet manyEntitySet,
    EdmEntityType oneEntityType, EdmEntityType manyEntityType) {
    var navPropertyInfo = new EdmNavigationPropertyInfo {
        TargetMultiplicity = EdmMultiplicity.One,
        Target = oneEntityType,
        ContainsTarget = false,
        OnDelete = EdmOnDeleteAction.None,
        Name = navTargetName
    };
    manyEntitySet.AddNavigationTarget(manyEntityType.AddUnidirectionalNavigation(navPropertyInfo), oneEntitySet);
}

最后,从WebApiConfig.Register调用GetEdmModel

Finally, call GetEdmModel from WebApiConfig.Register

config.MapODataServiceRoute("odata", null, GetEdmModel());

现在打电话给你的OData服务的一到多,许多到一导航从客户端,所有要善于用你的世界。在我的情况下调用看​​起来是这样的:

Now call your OData service's one-to-many and many-to-one navigations from your client and all should be good with your world. In my case the calls look like this:

一到多:

http://localhost:19215/Menus(c94f7f98-6987-e411-8119-984be10349a2)/MenuPermissions

多对一酮:

http://localhost:19215/MenuPermissions(ba0da52a-6c87-e411-8119-984be10349a2)/Menu

这答案假设你建立你的项目的其余部分,就像迈克·沃森建议在问题链接(该链接为3部分教程 - 你将需要<一个href=\"http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/create-an-odata-v4-endpoint\"相对=nofollow>遵循第1部分第!)。

This answer assumes you set up the rest of your project just like Mike Wasson suggests in the tutorial linked in the question (that link is to Part 3 - you will need to follow Part 1 first!).

这篇关于在获取和QUOT相关实体ASP.NET的WebAPI的OData v4的结果;没有HTTP资源发现,请求URI&QUOT匹配;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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