C# – Using reflection to get properties from subclass and base class

I came across this scenario recently where I needed to use reflection to get the properties declared in a subclass, and then get the properties declared in the base class. I had to handle these sets of properties differently. Here’s an example. Consider the following two classes. PersonBase is the base class. Driver is the … Read more

C# – How to read problem details JSON with HttpClient

Problem details (RFC7807) is a standardized error response format that has a Content-Type of application/problem+json, an error response code (i.e. 400 – Bad Request), and has a response body that looks like this: This can be extended to include any number of properties. The example shown above comes from the default way ASP.NET Core returns … Read more

C# – XML documentation parameter warnings (CS1571, CS1572, CS1573)

When you’re using XML documentation comments in your code, eventually the comments will get out of sync with the code. This is especially true for method parameters, which are documented by <param> tags. When you add, delete, or rename a parameter manually, it’s easy to forget to update the <param> tags. Fortunately, the compiler detects … Read more

ASP.NET Core – How to unit test a model validation attribute

You can directly unit test a model validation attribute by creating an instance of the attribute class, and then testing the two primary methods: IsValid() and FormatErrorMessage(). Here’s an example of unit testing a custom validation attribute: You can also test the built-in validation attributes (such as [Range], [RegularExpression], etc..), which is useful when you … Read more

ASP.NET Core – Create a custom model validation attribute

There are many built-in model validation attributes available – such as [Required] and [Range] – which you can use to handle most validation scenarios. When these aren’t sufficient, you can create a custom validation attribute with your own validation logic. I’ll show an example of how to do that. 1 – Subclass ValidationAttribute and implement … Read more

ASP.NET Core – The request matched multiple endpoints

Problem When you send a request to an endpoint, you get the following error response: Note: If you’re using Swagger, you may see this in the UI as a generic error: “Failed to load API definition … response status is 500.” If you look in the ASP.NET Core Web Server output in Visual Studio, you … Read more

C# – JSON object contains a trailing comma at the end which is not supported

Problem When you deserialize JSON, you get the following error: The JSON object contains a trailing comma at the end which is not supported in this mode. Change the reader options. JSON properties are separated with commas. A trailing comma is one that has no properties after it. Here’s an example of a trailing comma: … Read more

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

How to send a request with Postman

Postman is a great tool for web development. You can send a request to any web API, look at the response details, and reuse the the request later (which saves lots of effort!). This is useful for testing your own web API, or for when you’re integrating with a third-party API and want to try … Read more

SQL – GROUP BY

Here’s an example of how to use GROUP BY in a SQL query: This produces the following query results: GROUP BY allows you to group rows based on one or more columns and calculate aggregate values for the group. In the example above, the COUNT(*) aggregate function was used to show the number of rows … Read more