简体   繁体   中英

Asp.Net Core 2.0 ArgumentNullException: Value cannot be null. Parameter name: connectionString

I'm using ASP.NET Core 2.0, Visual Studio 2017 Enterprise, Version 15.5.4 and a local DB on my PC.

I'm working for the first time with database first and I have encountered the following problem:

unhandled exception occurred while processing the request.
ArgumentNullException: Value cannot be null.
Parameter name: connectionString.

The problem remains after reading and trying every possible suggestions along with possible solutions.

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {    
        services.AddMvc();
        services.AddDbContext<VideosContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseBrowserLink();
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseStaticFiles();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
        });
    }
}
    {
      "ConnectionString": {
        "DefaultConnection": "Server =(localdb)\\mssqllocaldb;Database=Videos;Trusted_Connection=True;MultipleActiveResultSets=true"
    },
    "Logging": {
      "IncludeScopes": false,
      "LogLevel": {
        "Default": "Warning"
      }
    }
}

public static void Main(string[] args)
{
     BuildWebHost(args).Run();
}

public static IWebHost BuildWebHost(string[] args) =>
   WebHost.CreateDefaultBuilder(args)
      .UseStartup<Startup>()
      .Build(); 

What am I doing wrong? Thank you in advance for any suggestion.

Does your app settings JSON file have the correct syntax?

{
    "ConnectionStrings": {
        "DefaultConnection": "Server =(localdb)\\mssqllocaldb;Database=Videos;Trusted_Connection=True;MultipleActiveResultSets=true"
    }
}

You are using GetConnectionString method to get the value of connection string from appsettings.json, so you need to take into account that method is just a shorthand for GetSection("ConnectionStrings")["name"] .

Since you have ConnectionString (singular) inside your appsettings.json it is expected to get such an error (ie there is no ConnectionString key).

If you want to keep the singular key, you should use GetSection("ConnectionString")["DefaultConnection"] instead. Otherwise, you need to update your appsettings.json file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM