Posts

Showing posts with the label Asp.Net Core Mvc

AddEntityFrameworkStores Can Only Be Called With A Role That Derives From IdentityRole In .NET Core 2.0

Answer : Long time since I asked this question, but here's how I deal with nowadays: Startup.cs services.AddIdentity<User, Role>() .AddEntityFrameworkStores<ApplicationDbContext>() .AddDefaultTokenProviders(); services.AddScoped<RoleManager<Role>>(); Entites: public class User : IdentityUser<int> { } public class Role : IdentityRole<int> { } For same issue, you can look at this:https://github.com/aspnet/Identity/issues/1364

ASP.NET Core MVC Mixed Route/FromBody Model Binding & Validation

Image
Answer : Install-Package HybridModelBinding Add to Statrup: services.AddMvc() .AddHybridModelBinder(); Model: public class Person { public int Id { get; set; } public string Name { get; set; } public string FavoriteColor { get; set; } } Controller: [HttpPost] [Route("people/{id}")] public IActionResult Post([FromHybrid]Person model) { } Request: curl -X POST -H "Accept: application/json" -H "Content-Type:application/json" -d '{ "id": 999, "name": "Bill Boga", "favoriteColor": "Blue" }' "https://localhost/people/123?name=William%20Boga" Result: { "Id": 123, "Name": "William Boga", "FavoriteColor": "Blue" } There are other advanced features. You can remove the [FromBody] decorator on your input and let MVC binding map the properties: [HttpPost("/test/{rootId}/echo/{id}...

Alternative Solution To HostingEnvironment.QueueBackgroundWorkItem In .NET Core

Answer : Update December 2019: ASP.NET Core 3.0 supports an easy way to implement background tasks using Microsoft.NET.Sdk.Worker. It's excellent and works really well. As @axelheer mentioned IHostedService is the way to go in .NET Core 2.0 and above. I needed a lightweight like for like ASP.NET Core replacement for HostingEnvironment.QueueBackgroundWorkItem, so I wrote DalSoft.Hosting.BackgroundQueue which uses.NET Core's 2.0 IHostedService. PM> Install-Package DalSoft.Hosting.BackgroundQueue In your ASP.NET Core Startup.cs: public void ConfigureServices(IServiceCollection services) { services.AddBackgroundQueue(onException:exception => { }); } To queue a background Task just add BackgroundQueue to your controller's constructor and call Enqueue . public EmailController(BackgroundQueue backgroundQueue) { _backgroundQueue = backgroundQueue; } [HttpPost, Route("/")] public IActionResult SendEmail([FromBody]emailRequest) { ...