简体   繁体   English

WebAPI服务设计

[英]WebAPI service design

I'm pretty comfortable with how Asp.NET MVC controllers worked when designing services. 我对设计服务时Asp.NET MVC控制器的工作方式非常满意。 However the new WebAPI controllers. 但是新的WebAPI控制器。 how am I supposed to design my services here? 我应该如何在这里设计服务?

Lets say we have 3 different ways to list eg Users. 可以说,我们有3种不同的方式来列出用户。

Get 10 latest , Get all, Get inactive or whatever. 获取10个最新消息,全部获取,闲置等。

none of these might need parameters. 这些都不需要参数。 so how would you solve this in WebAPI 那么如何在WebAPI中解决这个问题

IEnumerable<User> Get10Latest()
IEnumerable<User> GetAll()
IEnumerable<User> GetInactive()

That won't work since they have the same param signature. 因为它们具有相同的param签名,所以这行不通。 So what is the correct way to design this here? 那么在这里设计这个的正确方法是什么?

You can support multiple methods in one controller for a single HTTP method by using the action parameter. 您可以通过使用action参数在一个控制器中为单个HTTP方法支持多种方法。
Eg 例如

public class UsersController : ApiController
{

    [ActionName("All")]
    public HttpResponseMessage GetAll()
    {
        return new HttpResponseMessage();
    }

    [ActionName("MostIQ")]
    public HttpResponseMessage GetMostIQ()
    {
        return new HttpResponseMessage();
    }

    [ActionName("TenLatest")]
    public HttpResponseMessage GetTenLatest()
    {
        return new HttpResponseMessage();
    }

}

Unfortunately, I have not found a way to get a single controller to handle both with and without the action at the same time. 不幸的是,我还没有找到一种方法来让单个控制器同时处理和不处理动作。

eg 例如

public class UsersController : ApiController
{

    [ActionName("")]  // Removing this attribute doesn't help
    public HttpResponseMessage Get()
    {
        return new HttpResponseMessage();
    }

    [ActionName("All")]
    public HttpResponseMessage GetAll()
    {
        return new HttpResponseMessage();
    }

    [ActionName("MostIQ")]
    public HttpResponseMessage GetMostIQ()
    {
        return new HttpResponseMessage();
    }

    [ActionName("TenLatest")]
    public HttpResponseMessage GetTenLatest()
    {
        return new HttpResponseMessage();
    }

}

Being able to use a single controller for a collection resource and all of its subsets would be nice. 能够对收集资源及其所有子集使用单个控制器会很好。

Someone will probably be along and wrap me on the knuckles for this, but you need to configure your routing to handle the Gets. 可能有人会来帮我解决这个问题,但是您需要配置路由以处理Gets。 This is how I got it working with the above operations: 这就是我如何使用上述操作的方式:

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

So now your requests are mapped to the correct controller -> action via the route template. 因此,现在您的请求已通过路由模板映射到正确的控制器->操作。 Note that the new route needs to be registered first in WebApiConfig.cs. 请注意,新路由需要首先在WebApiConfig.cs中注册。 If you keep the old, default one. 如果保留旧版本,则默认为旧版本。

EDIT 编辑

Having re-read the question I realize I wasn't quite answering the design question. 重新阅读问题后,我意识到我还没有完全回答设计问题。 I would think that one way to go about it, from a REST perspective, would be to use a separate resource to expose the proper collections (Get10Latest for example) since I assume that there is a business reason for exposing that exact subset of data through the service. 我认为,从REST的角度出发,解决此问题的一种方法是使用单独的资源来公开适当的集合(例如,Get10Latest),因为我认为存在通过数据公开确切的数据子集的商业原因。服务。 In that case you'd expose that resource though a single Get in its own Controller (if that is the desired behaviour). 在这种情况下,您可以通过在其自己的Controller中使用单个Get来公开该资源(如果这是所需的行为)。

Well why not have urls like this: 那么为什么不拥有这样的网址:

GET /users
GET /users/latest
GET /users/inactive

Using routing you could route them to 使用路由,您可以将它们路由到

public classs UserController : ApiController
{
    public IEnumerable<User> Get(string mode)
    {
        // mode is in routing restricted to be either empty, latest, or inactive
     }
 }

Otherwise use multiple controllers. 否则,请使用多个控制器。 The use of action names in Web API is kind of a wrong way to about it. 在Web API中使用动作名称是错误的处理方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM