JsonConverter equivalent in using System.Text.Json

System.Text.Json now supports custom type converters in .NET 3.0 preview-7 and above. You can add converters that match on type, and use the JsonConverter attribute to use a specific converter for a property. Here’s an example to convert between long and string (because javascript doesn’t support 64-bit integers). public class LongToStringConverter : JsonConverter<long> { public … Read more

Unable to create an object of type ‘ApplicationDbContext’. For the different patterns supported at design time

I found the cause of this error could be multiple things in your code. For me at least, the best way was to add verbose in command. With that will be able to understand what is the problem. the verbose will display all steps of the execution. In visual studio use: add-migration Added_something -verbose For … Read more

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 use class fields with System.Text.Json.JsonSerializer?

In .NET Core 3.x, System.Text.Json does not serialize fields. From the docs: Fields are not supported in System.Text.Json in .NET Core 3.1. Custom converters can provide this functionality. In .NET 5 and later, public fields can be serialized by setting JsonSerializerOptions.IncludeFields to true or by marking the field to serialize with [JsonInclude]: using System.Text.Json; static … Read more

.NET Core 3 preview 4: ‘AddNewtonsoftJson’ is not defined

In order to switch ASP.NET Core 3.0 back to use JSON.NET, you will need to reference the Microsoft.AspNetCore.Mvc.NewtonsoftJson NuGet package. That will contain the AddNewtonsoftJson extension method. In C#, this would look like this: services.AddControllers() .AddNewtonsoftJson(); So assuming that I understand enough of F#, I would say that your call would be correct if you … Read more

System.Text.Json: How do I specify a custom name for an enum value?

This is not currently supported out of the box in .net-core-3.0, .net-5, .net-6.0 or .net-7.0. There is currently an issue Support for EnumMemberAttribute in JsonConverterEnum #31081[1] requesting this functionality. In the interim, you will need to create your own JsonConverterFactory that serializes enums with custom value names specified by attributes. If you need to round-trip … Read more

HTTP Error 500.31 – ANCM Failed to Find Native Dependencies in IIS

I have received the same error after upgrading my ASP.NET Core project from .NET Core 3.0 to 3.1 and installing Microsoft .NET Core 3.1.0 – Windows Server Hosting. Quick (but bad) fix changed the web.config handler from AspNetCoreModuleV2 to AspNetCoreModule and it worked ok. Good fix Find the underlying cause by inspecting Event Viewer. There … Read more

Get Current User in a Blazor component

There are three possibilities for getting the user in a component (a page is a component): Inject IHttpContextAccessor and from it access HttpContext and then User; need to register IHttpContextAccessor in Startup.ConfigureServices, normally using AddHttpContextAccessor. Edit: according to the Microsoft docs you must not do this for security reasons. Inject an AuthenticationStateProvider property, call GetAuthenticationStateAsync … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)