Category Archives: c#

.NET Core Api – Catch exceptions using a middleware

In .NET Core, we are starting to get used to the fact that nothing comes for free, and in the world of ultimate freedom of choice, every feature needs to be implemented by us. This includes error handling. In a … Continue reading

Posted in .NET Core, c#, Microsoft Azure | Tagged , , , | Leave a comment

.NET Core Catch Model Binding Exceptions

In .NET Core, if you create an API project, and you have an controller receiving an object you defined yourself, the controller will not be called if you use the wrong model, or if the model is not in a … Continue reading

Posted in .NET Core, c#, Microsoft Azure | Tagged , , | 1 Comment

ASP.Net Core API – “‘s’ is an invalid start of a value. Path: $ | LineNumber: 0 | BytePositionInLine: 0.”

If you create an ASP.Net Core API Controller, and you wish to create an endpoint that can accept any string, you would expect this to be correct: But this endpoint will only accept strings in quotes, so posting string will … Continue reading

Posted in .NET Core, c# | Tagged , , | Leave a comment

.NET Core API and CORS – allow POST from Javascript using Microsoft.AspNetCore.Cors

After hours of debugging, I finally managed to apply CORS correctly to my .NET Core 3.0 Application. Like so many other before me, I used this article as reference: Enable Cross-Origin Requests (CORS) in ASP.NET Core But I still managed … Continue reading

Posted in .net, .NET Core, c# | Tagged , , | 1 Comment

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the ‘await’ operator to the result of the call

This C# warning occurs if you call an async method from your non-async code. Imagine you have this imaginary async method: And you call the method from this imaginary non-async method: The compiler will warn you with the following message: … Continue reading

Posted in .net, .NET Core, c#, General .NET, Microsoft Azure | Tagged , , , | Leave a comment

.NET Core MVC Web API – control DateTime format in JSON output using JsonSerializerOptions

When creating API’s with .NET Core MVC, you can control the JSON output by adding JsonOptions to the controllers: This will ensure that when requesting application/json from a GET method, the format returned is JSON. You can then add Converters … Continue reading

Posted in .net, .NET Core, c# | Tagged , , , , | 1 Comment

Sitecore Publish Items using the PublishManager

Sitecore have two entries to publishing: Create a new Publisher and call Publish() or PublishAsync() Use the Sitecore.Publishing.PublishManager You should use the PublishManager. Sitecore introduced the Publishing Service which is an optional external publisher that is much faster than the … Continue reading

Posted in .net, c#, General .NET, Sitecore 8, Sitecore 9 | Tagged , , | 2 Comments

Sitecore publish certain items on save

My Sitecore solution have some items, where changes to certain fields are so critical that they need to be published immediately. So when my user presses the Save button, I will open a popup dialog, asking if I should publish … Continue reading

Posted in .net, c#, Sitecore 6, Sitecore 7, Sitecore 8, Sitecore 9 | Tagged , , | 1 Comment

C# HttpClient POST or PUT Json with content type application/json

The HttpClient is a nifty tool for getting and sending data to a URL, but it works differently from the old fashioned WebRequest class. The content type is added to the post data instead of added as a header parameter. … Continue reading

Posted in .net, .NET Core, c#, General .NET | Tagged , , , | Leave a comment

C# Newtonsoft serialize property that is an array and a string

IdentityServer4 has a strange way of returning roles. If there is only one role, the property is a string: But if there is more than one role, the property is an array of strings: If the property is a string, … Continue reading

Posted in .net, .NET Core, c#, General .NET | Tagged , , | Leave a comment