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

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

ASP.NET Core Web API exception handling

Quick and Easy Solution. Simply add this middleware before ASP.NET routing into your middleware registrations. app.UseExceptionHandler(c => c.Run(async context => { var exception = context.Features .Get<IExceptionHandlerPathFeature>() .Error; var response = new { error = exception.Message }; await context.Response.WriteAsJsonAsync(response); })); app.UseMvc(); // or .UseRouting() or .UseEndpoints() Enable Dependency Injection for logging and other purposes. Step 1. … Read more

asp.net core web api published in IIS after moved to different IIS server pc gives error 500.19 (0x8007000d)

To get a more detailed error message: Verify that the log directory exists at the path referenced by the web config. If it does not, create it. The path shown in your config would place the “logs” directory in the root folder of the deployed site. Verify that the application pool has write access to … Read more

Access the current HttpContext in ASP.NET Core

HttpContext.Current doesn’t exist anymore in ASP.NET Core but there’s a new IHttpContextAccessor that you can inject in your dependencies and use to retrieve the current HttpContext: public class MyComponent : IMyComponent { private readonly IHttpContextAccessor _contextAccessor; public MyComponent(IHttpContextAccessor contextAccessor) { _contextAccessor = contextAccessor; } public string GetDataFromSession() { return _contextAccessor.HttpContext.Session.GetString(*KEY*); } }

How to enable CORS in ASP.net Core WebAPI

Because you have a very simple CORS policy (Allow all requests from XXX domain), you don’t need to make it so complicated. Try doing the following first (A very basic implementation of CORS). If you haven’t already, install the CORS nuget package. Install-Package Microsoft.AspNetCore.Cors In the ConfigureServices method of your startup.cs, add the CORS services. … Read more

How to read AppSettings values from a .json file in ASP.NET Core

This has had a few twists and turns. I’ve modified this answer to be up to date with ASP.NET Core 2.0 (as of 26/02/2018). This is mostly taken from the official documentation: To work with settings in your ASP.NET application, it is recommended that you only instantiate a Configuration in your application’s Startup class. Then, … Read more