I was reading a book about Learning ASP.NET Core API when I run to a part saying:
We call GetCommandByID on our repository passing in the Id from our route, storing the result in a local variable.
Here is some text I thought you'd better have:
Then there are some explanations related to the picture above:
- The route to this controller action includes an additional route parameter, in this case the Id of the resource we want to retrieve; we can specify this in the HttpGet attribute as shown.
- The controller action requires an id to be passed in as a parameter (this comes from our route; see point 1) and returns an ActionResult of type Command.
- We call GetCommandByID on our repository passing in the Id from our route, storing the result in a local variable.
- We check to see if our result is null and, if so, return a 404 Not Found result.
- Otherwise if we have a Command object, we return a 200 OK and the result.
Now, I don't exactly know what the highlighted part above in number 3 mean? Could I rewrite number 3 as follow:
- We call GetCommandByID on our repository which is submitting the Id from our route, storing the result in a local variable.
If not, then Could anyone explain what the number 3 is saying to me?
CodePudding user response:
GetCommandById(int id)
is a method that requires an id
parameter. The method name is GetCommandById
.
When we call the method we must supply and id
parameter. We often refer to this as "passing in" the id
parameter. The method then has access to the id
and can use it as part of the work it performs. In this case the method ultimately returns an ActionResult<Command>
object.
CodePudding user response:
Here, public ActionResult<Command> GetCommandById(int id)
It receives Id from the route as a query parameter. Then we call the GetCommandById(int id)
in the repository by passing id as a parameter which got from the route. Then the value which returns from the GetCommandById(int id)
is stored in a variable called CommandItem
.