Setting Base Path using ConfigurationBuilder

configurationbuilder does not contain addenvironmentvariables
configurationbuilder does not contain a definition for addjsonfile
asp net set base path
configurationbuilder add
configurationbuilder setbasepath example
exception thrown system formatexception in microsoft extensions configuration fileextensions dll
net core add appsettings json
configuration does not contain a definition for getconnectionstring

I'm trying to set the application base path for a .Net web app I'm building. I keep getting errors on Configuration builder. This is the error I get.

DNX,Version=v4.5.1 error CS1061: 'ConfigurationBuilder' does not contain a definition for 'SetBasePath' and no extension method 'SetBasePath' accepting a first argument of type 'ConfigurationBuilder' could be found (are you missing a using directive or an assembly reference?)

I'm assuming I'll get the same error for my .AddJsonFile() and .AddEnvironmentVariables(). Did I do something wrong? Did I not add the right dependency to my project.json? I've enclosed my startup.cs and my project.json.

project.json

{
"version": "1.0.0-*",
"compilationOptions": {
"emitEntryPoint": true
},
"tooling": {
"defaultNamespace": "TripPlanner"
},

"dependencies": {
  "Microsoft.AspNet.StaticFiles":  "1.0.0-rc1-final",
  "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
  "Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
  "Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-rc1-final",
  "Microsoft.Framework.Configuration": "1.0.0-beta8",
  "Microsoft.Framework.DependencyInjection": "1.0.0-beta8"
  //"Microsoft.Extensions.PlatformAbstractions": "1.0.0-beta8"
},

"commands": {
  "web": "Microsoft.AspNet.Server.Kestrel"
},

"frameworks": {
  "dnx451": { },
  "dnxcore50": { }
},

"exclude": [
  "wwwroot",
  "node_modules"
],
"publishExclude": [
  "**.user",
  "**.vspscc"
 ]
}

startup.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.PlatformAbstractions;
using Microsoft.Framework.Configuration;
using Microsoft.Framework.DependencyInjection;
using TripPlanner.Services;



namespace TripPlanner
{
  public class Startup
  {
    public static IConfigurationRoot Configuration;

    public Startup(IApplicationEnvironment appEnv){
        var builder = new ConfigurationBuilder()
            .SetBasePath(appEnv.ApplicationBasePath)
            .AddJsonFile("config.json")
            .AddEnvironmentVariables();

        Configuration = builder.Build();
    }

    // This method gets called by the runtime. Use this method to add services to the container.
    // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
    public void ConfigureServices(Microsoft.Extensions.DependencyInjection.IServiceCollection services)
    {
        services.AddMvc();
        #if DEBUG
        services.AddScoped<IMailService, DebugMailService> ();
        #else
        services.AddScoped<IMailService, RealMailService> ();
        #endif
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app)
    {
       //app.UseDefaultFiles();
       app.UseStaticFiles();
       app.UseMvc(config =>
       {
           config.MapRoute(
               name: "Default",
               template: "{controller}/{action}/{id?}",
               defaults: new { controller  = "App", action = "Index"}
           );
       });

    }

    // Entry point for the application.
    public static void Main(string[] args) => Microsoft.AspNet.Hosting.WebApplication.Run<Startup>(args);
  }
}

The error is in the public startup function right near the top of startup.cs.

I was able to solve the issue. If you have not yet solved it, try the following in the project.json. Add the following:

"Microsoft.Extensions.Configuration.FileExtensions": "1.0.0-*",
"Microsoft.Extensions.Configuration.Json": "1.0.0-rc2-final"

and it should work

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  Here I am explaining ' How to set the base path in ConfigurationBuilder in Core 2.0.' Install the packages .NET Core configuration components are published under Microsoft.Extensions.Configuration.* packages on NuGet.

Not sure if anyone still runs into this issue but I was able to address this in a dotnetcore console project (netcoreapp2.0) via:

dotnet add package Microsoft.Extensions.Configuration.Json

How to SetBasePath in ConfigurationBuilder in ASP.NET Core 2.0 , Here I am explaining 'How to set the base path in ConfigurationBuilder in Core 2.0.' Install the packages .NET Core configuration components are  FileConfigurationExtensions.SetBasePath(IConfigurationBuilder, String) FileConfigurationExtensions.SetBasePath(IConfigurationBuilder, String) FileConfigurationExtensions.SetBasePath(IConfigurationBuilder, String) Method. Definition. Sets the FileProvider for file-based providers to a PhysicalFileProvider with the base path.

Try adding the following in your .csproj file:

<ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Configuration" Version="2.2.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.2.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />
</ItemGroup>

FileConfigurationExtensions.SetBasePath(IConfigurationBuilder , Sets the FileProvider for file-based providers to a PhysicalFileProvider with the base path. Setting Base Path using ConfigurationBuilder (5) . Add the following to your project.json:

If you`re running a .NET Core 1.x or .NET Standard 1.x, you should run this command:

dotnet add package Microsoft.Extensions.Configuration.Json -v 1.1.1

If your project is inside another folder:

dotnet add .\src\MyProject package Microsoft.Extensions.Configuration.Json -v 1.1.1

...where MyProject is the name of the .csproj file.

ASP.NET Core 2.1 Setting BasePath of appsettings.json for , I have these 2 solutions with this structure Solution1 UI Data error that says that the application can't find sone.json using the basepath of Solution2. [] args) { IConfigurationRoot configuration = new ConfigurationBuilder() . Now, when I new a ConfigurationBuilder using the empty ctor, the base path is not set obviously. Then, even if File.Exists(path) would return true, meaning the file exists, builder.AddJsonFile(path) still throws an InvalidOperationException stating "Unable to resolve path "construct this IConfigurationBuilder with a non-null BasePath".

Add the following to your project.json:

"Microsoft.Extensions.Configuration.FileExtensions": "1.0.0-*"

[Configuration] ConfigurationBuilder SetBasePath is now optional , With this change, we should be able to do this without setting base path. var builder = new ConfigurationBuilder() .AddJsonFile("config.json"); If  Re: ASP.NET Core 2.1 Setting BasePath of appsettings.json for application Nov 15, 2018 03:01 PM|Amos Ang|LINK. The expectation is that when Solution2 calls a function in Solution1 that refers to an appsettings.json file, the basepath that is used belongs to Solution1 instead of Solution2.

Configuration with .NET Core – csharp.christiannagel.com, Creating a new ConfigurationBuilder instance, a Fluent API is offered. method – to define the base path where the configuration files are read from now on. You can use multiple configuration files from different paths where  The preceding XML markup separates app settings from connection strings but requires all the keys in the web.config file to use the specified prefix. For example, the prefix AppSetting must be added to the ServiceID key ("AppSetting_ServiceID").

Accessing Configuration in .NET Core Test Projects, NET Core provides a clean configuration system and in ASP. GetIConfigurationRoot(string outputPath) { return new ConfigurationBuilder() . Notice that the code needs a basepath in order to find the appsettings.json file  Depending on the project and build configuration, the path to the bin folder may vary. For web apps, the base directory is usually the project's directory at debug time, or the directory you published the website into. Targeting .NET 4.x you'd probably use the AppDomain object like

Store ASP.NET Core Application Configuration in Multiple Files , This code creates ConfigurationBuilder and set's the base folder path where configuration files are stored. It then adds two JSON files and one  C# (CSharp) Microsoft.Extensions.Configuration ConfigurationBuilder.SetBasePath - 11 examples found. These are the top rated real world C# (CSharp) examples of Microsoft.Extensions.Configuration.ConfigurationBuilder.SetBasePath extracted from open source projects. You can rate examples to help us improve the quality of examples.

Comments
  • How t do this in EF Core 2.0 since now there is no project.json file ?
  • @ATHER: just add the two nugets the same way you add any other nugets.
  • @ATHER dotnet add package <packageName> where packageName in this case is Microsoft.Extensions.Configuration.FileExtensions, or in Visual Studio add nuget package
  • Thanks. This really helpful. I installed those dependencies from nuget. And the problem solved.
  • Thanks @bishop. Adding that in my .net core library solved my problem.
  • Microsoft.Extensions.Configuration.FileExtensions and Microsoft.Extensions.Configuration.Json are exactly the packages that are missing.
  • Thanks for the suggestion! I tried adding both to the project.json and then did a "dnu restore". But I'm still getting the same error in my Startup.cs. Any other ideas? I found the documentation for the exact function I'm using here. SetBasePath()