Ignore property when null using the new Net Core 3.0 Json

I’m looking at .Net Core 3.1, where this should ignore null values services.AddControllers().AddJsonOptions(options => { options.JsonSerializerOptions.IgnoreNullValues = true; }); While in .NET 5 and later, set JsonSerializerOptions.DefaultIgnoreCondition to JsonIgnoreCondition.WhenWritingNull: services.AddControllers().AddJsonOptions(options => { options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull; }); Note, the above isn’t per property/attribute, although there is an attribute which may be helpful JsonIgnoreAttribute. An alternative, to … Read more

Migrating .NET Core 2 to .NET Core 3: HttpContent does not contain a definition for “ReadAsAsync”

ReadAsAsync is a .NET Standard extension that’s actually shared between ASP.NET Core and ASP.NET Web Api (via a NuGet library). However, it uses JSON.NET to do the deserialization, and as of .NET Core 3.0, ASP.NET Core now uses System.Text.Json instead. As such, this library (and the extension it contains) is not included in the .NET … Read more

How to stop the localized Microsoft.CodeAnalysis.*.resources.dll files from getting published by ASP.NET Core?

Add this: <SatelliteResourceLanguages>en</SatelliteResourceLanguages> to the .csproj file: <Project Sdk=”Microsoft.NET.Sdk.Web”> <PropertyGroup> <TargetFramework>netcoreapp3.1</TargetFramework> <SatelliteResourceLanguages>en</SatelliteResourceLanguages> </PropertyGroup> As suggested, you can use none to exclude all of them: <SatelliteResourceLanguages>none</SatelliteResourceLanguages> and taking consideration languages do you want like english and spanish: <SatelliteResourceLanguages>en;es</SatelliteResourceLanguages> Works with VS2019 and other versions UPDATE 2021/2022: Still working with Visual Studio 2022 and .NET 6 <PropertyGroup> … Read more

.Net Core 3.0 JsonSerializer populate existing object

So assuming that Core 3 doesn’t support this out of the box, let’s try to work around this thing. So, what’s our problem? We want a method that overwrites some properties of an existing object with the ones from a json string. So our method will have a signature of: void PopulateObject<T>(T target, string jsonSource) … Read more

‘Could not load type ‘Microsoft.AspNetCore.Mvc.MvcJsonOptions’ from assembly ‘Microsoft.AspNetCore.Mvc.Formatters.Json, Version=3.0.0.0

I’m not sure if this solves OP’s problem, but this error also occurs when you use Swashbuckle 4 in .Net Core 3. The solution is to use Swashbuckle 5. (use command install-package Swashbuckle.AspNetCore) to have in .csproj <PackageReference Include=”Swashbuckle.AspNetCore” Version=”5.0.0″ /> Then you’ll need to upgrade it in Startup.cs. Generally that involves prefixing classes that … Read more

How to read configuration settings before initializing a Host in ASP .NET Core?

You can clear the default sources added by CreateDefaultBuilder then add a pre-built IConfiguration with the AddConfiguration extension method. public static void Main(string[] args) { //… var configuration = new ConfigurationBuilder() .AddEnvironmentVariables() .AddCommandLine(args) .AddJsonFile(“appsettings.json”) .Build(); //Do something useful with the configuration… var host = Host.CreateDefaultBuilder(args) .ConfigureAppConfiguration(builder => { builder.Sources.Clear(); builder.AddConfiguration(configuration); }) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); … Read more