的WebAPI又增加GET方法 [英] WebApi adding another Get Method

查看:256
本文介绍了的WebAPI又增加GET方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个pretty标准的WebAPI,做一些基本的CRUD操作。

I have a pretty standard WebApi that does some basic CRUD operations.

我想补充一些别样的查找,但我不能肯定它是如何假设做。

I'm trying to add some different kind of lookups but am not quite sure how it's suppose to be done.

下面是我目前FoldersController

Here's my FoldersController currently

public class FoldersController : ApiBaseController
{
    //using ninject to pass the unit of work in
    public FoldersController(IApiUnitOfWork uow)
    {
        Uow = uow;
    }

    // GET api/folders
    [HttpGet]
    public IEnumerable<Folder> Get()
    {
        return Uow.Folders.GetAll();
    }

    // GET api/folders/5
    public Folder Get(int id)
    {
        return Uow.Folders.GetById(id);
    }

    // POST api/folders
    public HttpResponseMessage Post(Folder folder)
    {
        Uow.Folders.Add(folder);
        Uow.Commit();

        var response = Request.CreateResponse(HttpStatusCode.Created, folder);

        // Compose location header that tells how to get this Folder
        response.Headers.Location = new Uri(Url.Link(WebApiConfig.DefaultRoute, new { id = folder.Id }));

        return response;
    }

    // PUT api/folders
    public HttpResponseMessage Put(Folder folder)
    {
        Uow.Folders.Update(folder);
        Uow.Commit();
        return new HttpResponseMessage(HttpStatusCode.NoContent);
    }

    // DELETE api/folders/5
    public HttpResponseMessage Delete(int id)
    {
        Uow.Folders.Delete(id);
        Uow.Commit();

        return new HttpResponseMessage(HttpStatusCode.NoContent);
    }
}

我想这样做是添加一个看起来像这样的方法

what I would like to do is add a method that looks something like this

public IEnumerable<Folder> GetChildFolders(int folderID)
{
     return Uow.Folders.GetChildren(folderID);
}

由于我已经在那里的标准Get方法,我不太知道如何做到这一点。

Since I already have the standard Get method in there, i'm not quite sure how to do so.

我最初以为我可以只添加像

I initially thought I could just add a new route..something like

routes.MapHttpRoute(
        name: "ActionAndIdRoute",
        routeTemplate: "api/{controller}/{action}/{id}",
        defaults: null,
        constraints: new { id = @"^/d+$" } //only numbers for id
        );

和刚刚添加的东西像一个ActionName标注我的方法 [ActionName(的GetChildren)]

And the just add something like an ActionName annotation to my method [ActionName("GetChildren")]

但没有飞起来。

我在正确的轨道上吗?我怎么做这样的事情而​​无需添加其他控制器?

Am I on the right track? How do I do something like this without adding another controller?

推荐答案

您可能不喜欢这个答案,但我觉得这是正确的。的WebAPI被设计成只有5通话,GET(一个项目/列表项),POST,PUT和每个实体类型的删除。这使得REST URL,例如文件夹/获取/ 5,文件夹/获取等。

You may not like this answer, but I feel it's the right one. WebAPI was designed to only have 5 calls, GET (one item / list items), POST, PUT and DELETE per entity type. This allows for REST URLs, such as Folders/Get/5, Folders/Get etc.

现在,在您的情况,您是在REST的角度想的 ChildFolders ,这我能理解没有不同的对象,但它们是不同的实体(ChildFolders / GET)等,我觉得这应该是另一种的WebAPI控制器。

Now, in your scenario, you're wanting ChildFolders, which I can understand aren't different objects, but they are different entities in terms of REST (ChildFolders/Get) etc. I feel this should be another WebAPI controller.

有修补中的HTTP路线来管理这个办法,但我不觉得它的Web API是如何设计的工作,它强制你遵循REST数据由实体型协议...不然为什么不只是使用.NET MVC控制器为你的AJAX调用?

There's ways of patching up the Http Routes to manage for this, but I don't feel it's how Web API was designed to work and it enforces you to follow REST data-by-entity-type protocols... otherwise why not just use .NET MVC Controllers for your AJAX calls?

这篇关于的WebAPI又增加GET方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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