HomeCloud ComputingTips on how to use route constraints in ASP.NET Core minimal APIs

Tips on how to use route constraints in ASP.NET Core minimal APIs




app.MapPut("/creator/{authorId:int:min(1)}/books/{rely:int:min(1)}",
    (int authorId, int rely) => $"Writer with Id {authorId} has written {rely} books.");

Instance of a route constraint in motion

Lastly, let’s look at how we are able to specify route constraints when passing objects and primitive information sorts (char, int, and so forth.) as parameters. Take into account the next HTTP POST endpoint, which provides a brand new creator and the variety of books authored to a database.


app.MapPost("/authors/{rely:int:min(1)}", (Writer creator, int rely) =>
  Outcomes.Okay($"1 new creator report of an creator who has written {rely} " +
    $"books has been added to the database...")
);

Let’s invoke this endpoint utilizing Postman. We’ll ship the next JSON information for our new creator within the physique of the request.


{
  "authorId": "1",
  "firstName": "Joydip",
  "lastName": "Kanjilal",
  "handle": "Hyderabad, India",
  "e-mail": "[email protected]",
  "cellphone": "1234567890"
}

Our route constraint requires us additionally to specify the rely of books written by the creator within the URL. Additional, it requires that the rely be a minimal integer worth of 1. Determine 1 reveals the output in Postman once we cross a rely of 5.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments