ASP.NET Core中CRUD调用的有效关键字是哪些?

huangapple go评论82阅读模式
英文:

Which Keywords are Valid for CRUD Calls in ASP.NET Core?

问题

我正在按照https://learn.microsoft.com/en-gb/training/modules/build-web-api-aspnet-core/7-crud中找到的教程进行学习。

其中的一个段落提到

> [HttpPost] 属性将使用 Create() 方法将发送到http://localhost:5000/pizza的HTTP POST请求进行映射。

现在我在某处读到,使用GetPostDelete作为Controller Action的名称会自动将该关键字映射到相应的操作(我可能对此感到困惑,所以如果我搞混了Entity Framework,可能需要一点澄清),例如:

  • public IActionResult Get() .. 将获取记录
  • public IActionResult Post(Pizza pizza) .. 将发布记录(插入到数据库中)

但我在文档中没有看到任何支持Create方法的地方,所以我不明白当它调用http://localhost:5000/pizza时,这段代码会如何创建一个新的披萨?/pizza如何映射到Create

我正在按照https://learn.microsoft.com/en-gb/training/modules/build-web-api-aspnet-core/7-crud中找到的教程进行学习,还查看了https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-7.0上的帮助文件,但没有回答我的问题。

英文:

I am going through the tutorial found at https://learn.microsoft.com/en-gb/training/modules/build-web-api-aspnet-core/7-crud.

One of the paragraphs state

> The [HttpPost] attribute will map HTTP POST requests sent to http://localhost:5000/pizza by using the Create() method.

Now i read somewhere that using the Get, Post, Delete as the name in the Controller Action would automatically map that keyword to the appropriate action (I may have confused myself on this so maybe a little clarity in case im getting mixed up with Entity Framework) for example

  • public IActionResult Get() .. would get the records
  • public IActionResult Post(Pizza pizza) .. would post a record (insert into the DB)

But i dont see anywhere in the documentation to support the Create method so im confused how this code

[HttpPost]
public IActionResult Create(Pizza pizza)
{            
    // This code will save the pizza and return a result
}

would create a new pizza when it calls http://localhost:5000/pizza? How does /pizza map to Create?

I am going through the tutorial found at https://learn.microsoft.com/en-gb/training/modules/build-web-api-aspnet-core/7-crud

Also viewed the help file at https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-7.0 which didnt answer my question.

答案1

得分: 1

这是关于控制器中Action()映射的工作流程。

  1. 在控制器中查找动词名称。例如:GET HTTP请求会查找控制器中是否定义了Get()动作。如果存在,则执行,否则执行以下操作。
  2. 查找以Get开头的动作。例如,如果控制器中不存在Get(),则查找以GetWHATEVERNAME()开头的动作,例如GetData()。如果没有以Get()开头的动作存在,则执行以下操作。
  3. 查找动作属性。我们可以为动作设置[HttpGet]、[HttpPost]、[HttpPut]等属性,以将特定动作与HTTP动词映射起来。

回答您的问题,创建动作具有HttpPost动词属性。因此,create与post之间有映射。因此,http://localhost:5000/pizza将向数据库添加一份披萨。
注意:将路由设置为复数形式,因为这是一种标准做法。

还有更多信息可在这里找到。

英文:

This is the workflow explaining the map of Action() in the controller.

  1. Searches for verb name in the controller. For example: a GET HTTP
    request looks for a Get() action defined in the controller. If it
    exists executes otherwise the following happens.
  2. Searches for an action starting with Get. For example if Get() does
    not exist in the controller then it looks for an action that starts
    with GetWHATEVERNAME() for example GetData(). If there is no action
    that starts with even Get(). If it exists executes otherwise the
    following happens.
  3. Searches for an action attribute. We can set
    [HttpGet],[HttpPost],[HttpPut],etc over an action that will map that
    specific action with a HTTP verb.

Straight to your question, the create action has a HttpPost verb attribute. So theres a map between create and post. Thus http://localhost:5000/pizza will add a pizza to the DB
NOTE: make the route plural since it is a standard.

There is also further info here

答案2

得分: 0

以下是已翻译的内容:

这是一个API控制器。描述这一部分的内容位于MS Learn: 使用ASP.NET Core中的HTTP动词属性进行属性路由的控制器操作路由上:

在构建REST API时,很少需要在动作方法上使用[Route(...)],因为该动作接受所有HTTP方法。最好使用更具体的HTTP动词属性,以精确指定API支持的内容。REST API的客户端应知道路径和HTTP动词与特定逻辑操作的映射关系。

REST API应该使用属性路由来将应用程序的功能建模为一组资源,其中操作由HTTP动词表示。这意味着许多操作,例如,对同一逻辑资源的GET和POST使用相同的URL。属性路由提供了需要仔细设计API的公共端点布局的控制级别。

因此,方法的命名方式并不重要。重要的是属性使其接受特定的动词。

因此,它将获取您的控制器名称(从PizzaController中获取Pizza),并将请求中的POST与Create方法匹配,因为该方法带有[HttpPost]标注。

现在进一步解答您问题中的评论:

您有一个控制器,可以位于客户端或服务器。在大多数情况下,它位于服务器。

在ASP.NET Core的上下文中,控制器始终在服务器上运行。

HTTP请求有5个部分:1)客户端发送一个请求,可以是Get或Post。Post表示请求的正文包含数据。2)服务器接收请求。3)服务器处理请求。4)服务器发送响应,可以是Get或Post。Get表示没有正文。在这种情况下,通常只是200 OK。Post表示有正文。5)客户端接收响应。

还有许多其他动词。大多数动词可以有正文,也可以省略正文。您可以在没有正文的情况下进行POST请求,也可以在有正文的情况下进行GET请求。请求有一个动词(GET、POST、DELETE等),而响应没有。

IActionResult是请求/响应正文中的数据。

嗯不。您可以返回带有正文或不带正文的IActionResult,但这与请求无关。

在这种情况下,创建方法是Microsoft的发明。

不,这里提到的“创建”是CRUD的一部分。

请求/响应的正文是序列化/反序列化为IActionResult。

什么?

URL(路由)确定用于处理消息正文的方法。

URL和动词。

正文中的实际数据是IActionResult。

什么?

英文:

It is an API controller. The section that describes this is on MS Learn: Routing to controller actions in ASP.NET Core: Attribute routing with Http verb attributes:

> When building a REST API, it's rare that you'll need to use [Route(...)] on an action method because the action accepts all HTTP methods. It's better to use the more specific HTTP verb attribute to be precise about what your API supports. Clients of REST APIs are expected to know what paths and HTTP verbs map to specific logical operations.
>
> REST APIs should use attribute routing to model the app's functionality as a set of resources where operations are represented by HTTP verbs. This means that many operations, for example, GET and POST on the same logical resource use the same URL. Attribute routing provides a level of control that's needed to carefully design an API's public endpoint layout.

So it doesn't matter how you name the methods. It's the attributes that make it accept a certain verb.

So it'll take your controller name (Pizza from PizzaController) and match the POST from the request to the Create method, as that's annotated with [HttpPost].

Now to further address the comments on your question:

> You have a controller which can either be at client or server. Most cases it is at server.

In the context of ASP.NET Core, controllers always run on the server.

> A HTTP request has 5 parts 1) Client sends a request which can either be a Get or Post. Post means the body of the request contains data. 2) Server receives the request. 3) Server processes the request 4) Server sends a response which can be a Get or Post. Get means there is no body. In this case it usually just a 200 OK. Post means there is a body 5) Client receives the response.

There are many more verbs. Most verbs can have a body, or omit one. You can POST without a body. You can GET with a body. The request has a verb (GET, POST, DELETE, ...), the response doesn't.

> The IActionResult is the data in the body of the request/response.

Well no. You can return an IActionResult with or without a body, but it has nothing to do with the request.

> The create method in this case is a Microsoft invention.

No, the "Create" mentioned here is from the C of CRUD.

> The body of the request/response is serialized/deserialized to the IActionResult.

What?

> The URL (route) determines what method is used to process the body of the message.

The URL and the verb.

> The actual data in the body is the IActionResult.

What?

huangapple
  • 本文由 发表于 2023年5月31日 23:25:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76375095.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定