How to SetBasePath in ConfigurationBuilder in Core 2.0

How can I set the base path in ConfigurationBuilder in Core 2.0. I have googled and found this question, this from Microsoft docs, and the 2.0 docs online but they seem to be using a version of Microsoft.Extension.Configuration from 1.0.0-beta8. I want to read appsettings.json. Is there a new way of doing this in Core … Read more

Return file in ASP.Net Core Web API

Problem I want to return a file in my ASP.Net Web API Controller, but all my approaches return the HttpResponseMessage as JSON. Code so far public async Task<HttpResponseMessage> DownloadAsync(string id) { var response = new HttpResponseMessage(HttpStatusCode.OK); response.Content = new StreamContent({{__insert_stream_here__}}); response.Content.Headers.ContentType = new MediaTypeHeaderValue(“application/octet-stream”); return response; } When I call this endpoint in my browser, … Read more

Is ConfigurationManager.AppSettings available in .NET Core 2.0?

I’ve got a method that reads settings from my config file like this: var value = ConfigurationManager.AppSettings[key]; It compiles fine when targeting .NET Standard 2.0 only. Now I need multiple targets, so I updated my project file with: <TargetFrameworks>netcoreapp2.0;net461;netstandard2.0</TargetFrameworks> But now, the compilation fails for netcoreapp2.0 with the following error message: Error CS0103 The name … Read more

Unable to find testhost.dll. Please publish your test project and retry

I have a simple dotnet core class library with a single XUnit test method: TestLib.csproj: <Project Sdk=”Microsoft.NET.Sdk”> <PropertyGroup> <TargetFramework>netstandard2.0</TargetFramework> </PropertyGroup> <ItemGroup> <PackageReference Include=”Microsoft.NET.Test.SDK” Version=”15.9.0″ /> <PackageReference Include=”xunit” Version=”2.4.1″ /> <PackageReference Include=”xunit.runner.console” Version=”2.4.1″> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> <PrivateAssets>all</PrivateAssets> </PackageReference> <PackageReference Include=”xunit.runner.visualstudio” Version=”2.4.1″> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> <PrivateAssets>all</PrivateAssets> </PackageReference> <PackageReference Include=”xunit.runners” Version=”2.0.0″ /> </ItemGroup> </Project> … Read more

.NET Core DI, ways of passing parameters to constructor

Having the following service constructor public class Service : IService { public Service(IOtherService service1, IAnotherOne service2, string arg) { } } What are the choices of passing the parameters using .NET Core IOC mechanism services.AddSingleton<IOtherService , OtherService>(); services.AddSingleton<IAnotherOne , AnotherOne>(); services.AddSingleton<IService>(x => new Service( services.BuildServiceProvider().GetService<IOtherService>(), services.BuildServiceProvider().GetService<IAnotherOne >(), “”)); Is there any other way ? 4 … Read more

Using ‘UseMvc’ to configure MVC is not supported while using Endpoint Routing

I had an Asp.Net core 2.2 project. Recently, I changed the version from .net core 2.2 to .net core 3.0 Preview 8. After this change I see this warning message: using ‘UseMvc’ to configure MVC is not supported while using Endpoint Routing. To continue using ‘UseMvc’, please set ‘MvcOptions.EnableEndpointRouting = false’ inside ‘ConfigureServices’. I understand … Read more

How to read values from the querystring with ASP.NET Core?

I’m building one RESTful API using ASP.NET Core MVC and I want to use querystring parameters to specify filtering and paging on a resource that returns a collection. In that case, I need to read the values passed in the querystring to filter and select the results to return. I’ve already found out that inside … Read more