ASP.NET Core – API model validation attributes

It’s always a good idea to validate data coming into your web API. There are two steps you can do to guard against invalid data: Declare your model properties with the proper types. (ex: string, DateTime, int). Use model validation attributes. The main built-in ones are [Required], [Range], [StringLength], and [RegularExpression]. Here’s an example of … Read more

ASP.NET Core – Getting query string values

The ASP.NET Core framework automatically parses query strings (i.e. ?name=Dune&year=2021) into HttpContext.Request.Query and maps the query string values to action parameters (if you’ve added them). You can get the mapped query string values by adding action parameters, like this: Or you can use HttpContext.Request.Query directly (which is useful in many scenarios): This can either return … Read more