Change the JSON serialization settings of a single ASP.NET Core controller

ASP.NET Core 3.0+ You can achieve this with a combination of an Action Filter and an Output Formatter. Things look a little different for 3.0+, where the default JSON-formatters for 3.0+ are based on System.Text.Json. At the time of writing, these don’t have built-in support for a snake-case naming strategy. However, if you’re using Json.NET … Read more

Compile a .NET Core application as an EXE file using Visual Studio 2017

Update 2019: .NET Core 3.0+ projects will now include an executable for the platform you build on by default. This is just a shim executable and your main logic is still inside a .dll file. But .NET Core 3.0 also introduced single-file deployments so deploying with dotnet publish -r win-x64 -p:PublishSingleFile=True –self-contained false will create … Read more

dotnet publish doesn´t publish correct appsettings.{env.EnvironmentName}.json

Update: For current (new) .csproj format the CopyToPublishDirectory attribute should be used. It determines whether to copy the file to the publish directory and can have one of the following value: Always, PreserveNewest Never So add next section into your .csproj: <ItemGroup> <None Include=”appsettings.Production.json” CopyToPublishDirectory=”Always” /> </ItemGroup> Look into @nover answer and SO Exclude or … Read more

Auto Versioning in Visual Studio 2017 (.NET Core)

Add <Deterministic>False</Deterministic> inside a <PropertyGroup> section  of .csproj The workaround to make AssemblyVersion * working is described in “Confusing error message for wildcard in [AssemblyVersion] on .Net Core #22660” Wildcards are only allowed if the build is not deterministic, which is the default for .Net Core projects. Adding <Deterministic>False</Deterministic> to csproj fixes the issue. The reasons why .Net Core … Read more

Determine Operating System in .NET Core

Method System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform() Possible Argument OSPlatform.Windows OSPlatform.OSX OSPlatform.Linux Example bool isWindows = System.Runtime.InteropServices.RuntimeInformation .IsOSPlatform(OSPlatform.Windows); Update Thanks to the comment by Oleksii Vynnychenko You can get the operating systems name and version as a string using var osNameAndVersion = System.Runtime.InteropServices.RuntimeInformation.OSDescription; E.g. osNameAndVersion would be Microsoft Windows 10.0.10586

.NET Core MVC Page Not Refreshing After Changes

In ASP.NET Core 3.0 and higher, RazorViewEngineOptions.AllowRecompilingViewsOnFileChange is not available. Surprised that refreshing a view while the app is running did not work, I discovered the following solution: Add Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation NuGet package to the project Add the following in Startup.cs: services.AddControllersWithViews().AddRazorRuntimeCompilation(); Here’s the full explanation for the curious.

Command dotnet ef not found

To install the dotnet-ef tool, run the following command: .NET 7 dotnet tool install –global dotnet-ef .NET 6 dotnet tool install –global dotnet-ef –version 6.* .NET 5 dotnet tool install –global dotnet-ef –version 5.* .NET Core 3 dotnet tool install –global dotnet-ef –version 3.* For more information about the history of dotnet-ef, see the announcement … Read more

How to add the same column to all entities in EF Core?

Your question title is about adding the same properties to multiple entities. However, you actually know how to achieve this (use a base type) and your actual question is how to ensure that these properties come last in the generated tables’ columns. Although column order shouldn’t really matter nowadays, I’ll show an alternative that you … Read more