Problems with connectionString in ASP.NET C# CORE

Asked

Viewed 215 times

0

I’m new to ASP.NET with C# and have a problem accessing the information on my web.config.

This is the connection tag to the database:

<connectionStrings>
    <add name="someConnection" connectionString="SomeInformation" providerName="System.Data.SqlClient" />
</connectionStrings>`

This is the function I’m using to try to retrieve the information:

string constr = ConfigurationManager.ConnectionStrings["someConnection"].ConnectionString;

This returns me the error "Using the generic Configutationmanager type requires 1 type argument", but I have tried typing the configurationManager and always returns the error "Configurationmanager does not contain a definition for Connectionstrings", regardless of the "T" that I put.

I need help, thank you.

  • 1

    Hello, Asp.net-core and web.config? you shouldn’t be putting your settings on appsettings.json? Have a look at the documentation and tutorial https://docs.microsoft.com/pt-br/aspnet/core/tutorials/razor-pages/sql?view=aspnetcore-3.1&tabs=visual-studio

  • You could add the *.csproj file of your project to confirm the version of . net that is used?

  • Sorry, I got the connection from the wrong project, this is the correct one from the json: "Connectionstring": "Host= localhost; user= user; pass=pass"

3 answers

0

In accordance with that response from the OS, you need to add the nuget package System.Configuration.ConfigurationManager to your project.

After that, you access the ConfigurationManager through the namespace System.Configuration.

Example:

var conString = System.Configuration.ConfigurationManager.ConnectionStrings["con"].ConnectionString;

0

ASP.NET core has a better function for finding Connection strings. I suggest using: Configuration.GetConnectionString("{NomeDaSuaConnectionString}")

You have more information on how to connect your ASP.NET core applications to the microsoft documentation at this link: link

0


The correct command using lib System.Configuration 4.0.0 is

ConfigurationManager.AppSettings[paramName];

Browser other questions tagged

You are not signed in. Login or sign up in order to post.