How do you unit test ASP.NET Core MVC Controllers that return anonymous objects?

Original idea from this answer with a more generic approach. Using a custom DynamicObject as a wrapper for inspecting the value via reflection there was no need to add the InternalsVisibleTo public class DynamicObjectResultValue : DynamicObject, IEquatable<DynamicObjectResultValue> { private readonly object value; public DynamicObjectResultValue(object value) { this.value = value; } #region Operators public static bool … Read more

Sharing Cookies Between Two ASP.NET Core Applications

This is documented at Sharing cookies among apps with ASP.NET and ASP.NET Core. There is also a Cookie Sharing App Sample available. In order to share cookies, you create a DataProtectionProvider in each app and using a common/shared set of keys between the apps. .AddCookie(options => { options.Cookie.Name = “.SharedCookie”; options.Cookie.Domain = “example.com”; options.DataProtectionProvider = … Read more

Run database migrations using Entity Framework core on application start

You can do this in the config methods in your Startup.cs. The simplest way is like this: public void ConfigureServices(IServiceCollection services) { services.AddDbContext<ApplicationDbContext>(); // add other services } public void Configure(IApplicationBuilder app, ApplicationDbContext db) { db.Database.Migrate(); // configure other services }

MVC6 Dropdownlist of Countries

The way I make my dropdowns is somewhat similar except that in my ViewModel, my property is of type SelectList instead of an IEnumerable<>. public class HomeViewModel { public string CountryCode { get; set; } public SelectList CountryList { get; set; } } Then in the controller I get the data and convert it to … Read more

ASP.NET Core HTTPRequestMessage returns strange JSON message

According to this article, ASP.NET Core MVC does not support HttpResponseMessage-returning methods by default. If you want to keep using it, you can, by using WebApiCompatShim: Add reference to Microsoft.AspNetCore.Mvc.WebApiCompatShim to your project. Configure it in ConfigureServices(): services.AddMvc().AddWebApiConventions(); Set up route for it in Configure(): app.UseMvc(routes => { routes.MapWebApiRoute( name: “default”, template: “{controller=Home}/{action=Index}/{id?}”); });

How to start Quartz in ASP.NET Core?

TL;DR (full answer can be found below) Assumed tooling: Visual Studio 2017 RTM, .NET Core 1.1, .NET Core SDK 1.0, SQL Server Express 2016 LocalDB. In web application .csproj: <Project Sdk=”Microsoft.NET.Sdk.Web”> <!– …. existing contents …. –> <!– add the following ItemGroup element, it adds required packages –> <ItemGroup> <PackageReference Include=”Quartz” Version=”3.0.0-alpha2″ /> <PackageReference Include=”Quartz.Serialization.Json” … Read more