Asp.NET的WebAPI公约为基础的方法网址/路由查询 [英] Asp.NET WebApi Convention based approach Url/Route query

查看:116
本文介绍了Asp.NET的WebAPI公约为基础的方法网址/路由查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道的最佳实践为需要返回资源的一个子属性一个asp.net的WebAPI约定基于REST服务的方式前进。

I'm not sure of the 'best practice' way forward for an asp.net webapi convention based rest service that needs to return a 'sub property' of a resource.

例如:

UsersController

public User Get(int id) { ... } //returns named user via /api/v1/Users/23

但是如果我想返回给定用户的角色集合我想我会 / API / V1 /用户/ 23 /角色

如果您在使用我的API,你会认为这是可以接受的?

If you were using my api would you consider this acceptable?

如果它是可以接受的,我会和routeTemplate方法签名是什么样子(对不起,如果这是显而易见的 - 我今天真的很困惑我自己)

If it is acceptable, what would my routeTemplate and method signature look like (sorry if this is obvious - I've really confused myself today)

所有网页API的例子我能看到的是过于简单,只需使用DELETE,PUT,POST和二送的 - 似乎没有覆盖子一样东西的属性(如上)或部分缓解
/ API / V1 /用户/ 23场= ID,名称 - ?如果有人一个很好的例子,都知道在那里,将是真棒

All the web api examples I can find are too simple and just use DELETE,PUT,POST and two GET's - none seem to cover anything like sub properties (as above) or Partial Responses /api/v1/Users/23?fields=id,name - If anybody knows of a good example out there that would be awesome.

非常感谢

推荐答案

角色

角色看起来像一个非常明智的子资源。

Roles looks like a very sensible sub resource.

假设你将要列出使用HTTP动词从用户删除角色......那么你可以使用两个控制器,一个用于用户和另一个角色。然后,您的路线看起来像这样:

Assuming you will want to list and remove roles from the user using http verbs... then you can use two controllers, one for Users and another for Roles. Your routes would then look like so:

config.Routes.MapHttpRoute(
            name: "UserApi",
            routeTemplate: "api/v1/Users/{userId}/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/v1/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

您的角色然后,控制器将有这样的方法:

Your roles controller would then have methods like:

public class RolesController : ApiController
{
    // GET api/v1/users/1/roles
    public IEnumerable<Role> Get(int userId)
    {
        //
    }

    // GET api/v1/users/1/roles/1
    public IEnumerable<Role> Get(int userId, int id)
    {
        //
    }

}

角色作为用户的部分响应

为了什么格式和标准用于请求:

apigee办在其网站上免费的电子书,他们做出的设计建议的约观察现有的API。

apigee do a free eBook on their site where they make design recommendation's and observations about existing API's.

他们描述了从LinkedIn,Facebook和谷歌部分反应的例子。它是覆盖在自己的博客之一<一个href=\"http://blog.apigee.com/detail/restful_api_design_can_your_api_give_developers_just_the_information\"相对=nofollow>这里和这里在他们的书

They describe partial response examples from LinkedIn, Facebook and Google. It is covered in one of their blogs here and in their book here.

如何使用ASP.NET的WebAPI

假设使用JSON作为内容类型,那么类似的问题之前已经<一个问href=\"http://stackoverflow.com/questions/13606675/asp-net-web-api-partial-response-json-serialization\">ASP.NET网页API部分缓解JSON序列,总之你需要通过查询参数为?字段=或诸如此类到定制 ContractResolver

Assuming the use of JSON as the content type, then a similar question has been asked before ASP.NET Web API partial response Json serialization, in short you will need to pass the query param for "?Fields=" or such like into a custom ContractResolver.

这篇关于Asp.NET的WebAPI公约为基础的方法网址/路由查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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