简体   繁体   English

REST API 具有多种获取方法 [暂停]

[英]REST API with multiple Get methods [on hold]

I am learning C# REST API implementation and i have a few questions about multiple Get methods.我正在学习 C# REST API 实现,我对多个 Get 方法有一些疑问。

Below is my sample code from NET Core 2.1 project下面是我来自 NET Core 2.1 项目的示例代码

Questions:问题:

  1. Can i implement multiple Get methods in same controller?我可以在同一个 controller 中实现多个 Get 方法吗?

  2. Is my Configure code to MapRoute correct?我的 MapRoute 配置代码是否正确?

  3. What URL should be to call GetByName ( https://localhost:44306/api/Books/author=Smith ) method?什么 URL 应该调用 GetByName ( https://localhost:44306/api/Books/author=Smith ) 方法?

  4. is it normal to implement multiple GET,(POST) etc. methods in the same Controller在同一个Controller中实现多个GET,(POST)等方法正常吗

BooksController-----图书控制器-----

    [Route("")]
    [HttpGet]
    public ActionResult<IEnumerable<Book>> Get()
    {
        return _bookService.GetAll;
    }


    [Route("{name}")]
    [HttpGet]
    public ActionResult<Book> GetByName(string name)
    {
        var book = _bookService.GetByAuthor(name);

        if (book == null)
            return NotFound();

        return book;
    }

void Configure(IApplicationBuilder app, IHostingEnvironment env) ---无效配置(IApplicationBuilder 应用程序,IHostingEnvironment env)---

        app.UseMvc(routes => {
            routes.MapRoute(name: "author", template: 
           "api/{controller}/{GetByName}");
        });

You most certainly can have multiple gets on a single controller.您当然可以在单个 controller 上获得多个。 It's quite common behaviour.这是很常见的行为。

In your instance, to hit your GetByName you would need to call it with https://localhost:44306/api/Books/Smith , although I personally would recommend losing the route requirement and hit it by using https://localhost:44306/api/Books?name=Smith在您的实例中,要点击您的 GetByName,您需要使用https://localhost:44306/api/Books/Smith调用它,尽管我个人建议丢失路线要求并使用https://localhost:44306来调用它/api/Books?name=史密斯

EG例如

    [Route("")]
    [HttpGet]
    public ActionResult<Book> GetByName(string name)
    {
        var book = _bookService.GetByAuthor(name);

        if (book == null)
            return NotFound();

        return book;
    }

It is perfectly normal to have multiple GETs on your controller.在 controller 上有多个 GET 是完全正常的。 You would normally have one for listing, where you would call youapi.com/api/Books then maybe pass some filtering options in and expect a list in return.您通常会列出一个列表,您可以在其中调用youapi.com/api/Books然后可能会传递一些过滤选项并期望得到一个列表作为回报。 Then you can have a get individual, for example yourapi.com/api?id=1234 that will return the single item that you highlighted.然后,您可以有一个 get 个人,例如yourapi.com/api?id=1234 ,它将返回您突出显示的单个项目。

The main thing to consider is that your controller should be unique to the entity.要考虑的主要事情是您的 controller 对于实体应该是唯一的。 Create a new controller for a different entity (eg BooksController, AuthorController, PublisherController).为不同的实体(例如 BooksController、AuthorController、PublisherController)创建一个新的 controller。

This is a great read on Restful Api design - https://link.medium.com/gBqAQPLT6Z这是关于 Restful Api 设计的精彩读物 - https://link.medium.com/gBqAQPLT6Z

Can I implement multiple Get methods in same controller?我可以在同一个 controller 中实现多个 Get 方法吗?

Yes.是的。 They just need to have unique routes.他们只需要有独特的路线。 (There might be some more nuance to that rule, but it's the broad advice). (该规则可能有更多细微差别,但这是广泛的建议)。

Is my Configure code to MapRoute correct?我的 MapRoute 配置代码是否正确?

Doesn't look like it.看起来不像。 GetByName is the name of the method not the route to the action . GetByName方法的名称,而不是动作的路径。 Actually the easiest thing to do is just not have any named routes at all in your Startup file:实际上,最简单的做法就是在您的启动文件中根本没有任何命名路由:

app.UseMvc(); // that's it.

What URL should be to call GetByName ( https://localhost:44306/api/Books/author=Smith ) method?什么 URL 应该调用 GetByName ( https://localhost:44306/api/Books/author=Smith ) 方法?

Assuming the full class looks like this:假设完整的 class 如下所示:

[Route("api/Books")]
public class Books
{
    [HttpGet("")]
    public ActionResult<IEnumerable<Book>> Get()

    [HttpGet("{name}")]
    public ActionResult<Book> GetByName([FromRoute] string name)   
}

Then to call the Get method you would call /api/Books and for the GetByName method you would call /api/Books/abcd .然后要调用Get方法,您将调用/api/Books ,对于GetByName方法,您将调用/api/Books/abcd

Is it normal to implement multiple GET,(POST) etc. methods in the same Controller?在同一个Controller中实现多个GET,(POST)等方法正常吗?

Sure.当然。 Use controllers (in my opinion) to logically group similar endpoints by some criteria, usually by the resource type they interact with.使用控制器(在我看来)按照某些标准对相似的端点进行逻辑分组,通常是按照它们与之交互的资源类型。

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

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