How can I get my .NET Core 3 single file app to find the appsettings.json file?

I found an issue on GitHub here titled PublishSingleFile excluding appsettings not working as expected. That pointed to another issue here titled single file publish: AppContext.BaseDirectory doesn’t point to apphost directory In it, a solution was to try Process.GetCurrentProcess().MainModule.FileName The following code configured the application to look at the directory that the single-executable application was … Read more

Confused with FromBody in ASP.NET Core

For anyone seeing this issue .net core 3 – you need to add the [ApiController] to the controller where you extend ControllerBase. The [FromBody] is only needed if you’re doing an MVC controller. This causes the body to get automatically processed in the way you’re expecting. Microsoft documentation for the ApiController attribute

Getting Scope Validating error in Identity Server 4 using JavaScript Client in asp.net core

While your client (application) is configured or allowed to request the openid resource (or scope), your identity server is not configured for the openid identity resource You need to add it as an identity resource similar to how its done here and have a method that returns all your identity resources that you want to … Read more

My Angular application doesn’t hit breakpoint when debugging?

Here’s what was going on! It has to do with the path to the project in the .vscode file and the need to debug two separate project/s folders at one time! ${workspaceFolder} I have two folders for the app Yogabandy-SPA (Angular app) Yogabandy.API (ASP.Net Core Web API) I thought the best place for the .vscode … Read more

How can i configure JSON format indents in ASP.NET Core Web API

.NET Core 2.2 and lower: In your Startup.cs file, call the AddJsonOptions extension: services.AddMvc() .AddJsonOptions(options => { options.SerializerSettings.Formatting = Formatting.Indented; }); Note that this solution requires Newtonsoft.Json. .NET Core 3.0 and higher: In your Startup.cs file, call the AddJsonOptions extension: services.AddMvc() .AddJsonOptions(options => { options.JsonSerializerOptions.WriteIndented = true; }); As for switching the option based on … Read more

ASP.NET Core Authorize attribute not working with JWT

Found it! The main problem is in this line: services.AddMvcCore().AddJsonFormatters(options => options.ContractResolver = new CamelCasePropertyNamesContractResolver()); I noticed that by switching from AddMvcCore() to AddMvc(), the authorization suddenly started working! After digging through the ASP.NET source code, to see what AddMvc() does, I realized that I need a second call, to IMvcBuilder.AddAuthorization(). services.AddMvcCore() .AddAuthorization() // Note … Read more

Using Dependency Injection with .NET Core Class Library (.NET Standard)

You don’t have to do anything in your class library. Only the main application has a composition root (earliest point in an application lifecycle you can set up your object graph). This happens in Startup.cs in your ASP.NET Core application. There you also register your dependencies: services.AddScoped<IUserManager,UserManager>(); That’s it. Class libraries don’t have a composition … Read more

Async provider in .NET Core DI

Although it is theoretically possible to use async/await during object resolution, you should consider the following constraints: Constructors can’t be asynchronous, and Construction of object graphs should be simple, reliable and fast Because of these constraints, it’s best to postpone everything that involves I/O until after the object graph has been constructed. So instead of … Read more

How to implement a “pure” ASP.NET Core Web API by using AddMvcCore()

What is the difference between AddMvc() and AddMvcCore()? The first thing key thing to understand is that AddMvc() is just a pre-loaded version of AddMvcCore(). You can see the exact implementation of the AddMvc() extension at the GitHub repository. I like using default VS templates as much as the next guy, but sometimes you need … Read more

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