asp.net-core-mvc
Unity with ASP.NET Core and MVC6 (Core)
So after some research I came up with the following solutions to my problems: Use Unity with ASP To be able to use Unity with ASP I needed a custom IServiceProvider (ASP Documentation) so I wrote a wrapper for the IUnityContainer which looks like this public class UnityServiceProvider : IServiceProvider { private IUnityContainer _container; public … Read more
asp.net conditionally disable a tag helper (textarea)
It is actually very simple, the disable attribute is already working as you want – you can pass in a boolean value: <textarea asp-for=”Doc” disabled=”@Model.MustDisable”></textarea> if false the disabled attribute is not rendered: <textarea></textarea> if true the disabled attribute is set to “disabled”: <textarea disabled=”disabled”></textarea>
ASP.NET 5 Authorize against two or more policies (OR-combined policy)
Not the way you want; policies are designed to be cumulative. For example if you use two separate attributes then they must both pass. You have to evaluate OR conditions within a single policy. But you don’t have to code it as ORs within a single handler. You can have a requirement which has more … Read more
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