ASP.NET Core form POST results in a HTTP 415 Unsupported Media Type response

For forms, use the [FromForm] attribute instead of the [FromBody] attribute. The below controller works with ASP.NET Core 1.1: public class MyController : Controller { [HttpPost] public async Task<IActionResult> Submit([FromForm] MyModel model) { //… } } Note: [FromXxx] is required if your controller is annotated with [ApiController]. For normal view controllers it can be omitted.

Getting value from appsettings.json in .net core

Program and Startup class .NET Core 2.x You don’t need to new IConfiguration in the Startup constructor. Its implementation will be injected by the DI system. // Program.cs public class Program { public static void Main(string[] args) { BuildWebHost(args).Run(); } public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>() .Build(); } // Startup.cs public class Startup … Read more

How to get the Development/Staging/production Hosting Environment in ConfigureServices

You can easily access it in ConfigureServices, just persist it to a property during Startup method which is called first and gets it passed in, then you can access the property from ConfigureServices. public Startup(IWebHostEnvironment env, IApplicationEnvironment appEnv) { …your code here… CurrentEnvironment = env; } private IWebHostEnvironment CurrentEnvironment{ get; set; } public void ConfigureServices(IServiceCollection … Read more

Dependency Injection error: Unable to resolve service for type while attempting to activate, while class is registered

To break down the error message: Unable to resolve service for type ‘WebApplication1.Data.BloggerRepository’ while attempting to activate ‘WebApplication1.Controllers.BlogController’. That is saying that your application is trying to create an instance of BlogController but it doesn’t know how to create an instance of BloggerRepository to pass into the constructor. Now look at your startup: services.AddScoped<IBloggerRepository, BloggerRepository>(); … Read more

How to register multiple implementations of the same interface in Asp.Net Core?

I did a simple workaround using Func when I found myself in this situation. Firstly declare a shared delegate: public delegate IService ServiceResolver(string key); Then in your Startup.cs, setup the multiple concrete registrations and a manual mapping of those types: services.AddTransient<ServiceA>(); services.AddTransient<ServiceB>(); services.AddTransient<ServiceC>(); services.AddTransient<ServiceResolver>(serviceProvider => key => { switch (key) { case “A”: return serviceProvider.GetService<ServiceA>(); … Read more

Resolving instances with ASP.NET Core DI from within ConfigureServices

The IServiceCollection interface is used for building a dependency injection container. After it’s fully built, it gets composed to an IServiceProvider instance which you can use to resolve services. You can inject an IServiceProvider into any class. The IApplicationBuilder and HttpContext classes can provide the service provider as well, via their ApplicationServices or RequestServices properties … Read more

How do you create a custom AuthorizeAttribute in ASP.NET Core?

The approach recommended by the ASP.Net Core team is to use the new policy design which is fully documented here. The basic idea behind the new approach is to use the new [Authorize] attribute to designate a “policy” (e.g. [Authorize( Policy = “YouNeedToBe18ToDoThis”)] where the policy is registered in the application’s Startup.cs to execute some … Read more

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