Creating a line in the web.config file and using it in web.api

I am new to the world of web development and I would like to create a variable in the web.config file so that I can use it in the .NET part of web.api

I found the following lessons on how to do this:

Setting the connection string in ASP.NET for SQL SERVER

and

http://www.connectionstrings.com/Articles/Show/store-connection-string-in-web-config

I have the following question: I do not have a database to connect the string (I will use it only in the web configuration so that I can easily change the string without having to go through the code. Using it as follows:

<add name="ConnStringDb1" connectionString="Data Source=localhost;Initial Catalog=YourDataBaseName;Integrated Security=True;" providerName="System.Data.SqlClient" /> 

What do I need in connectionString and providerName ?

+4
source share
3 answers

If I understand what you want to do, it looks like you don’t want to use the connection string at all. Instead, use the Application Settings sections of your web.config file. for instance

 <configuration> <system.web> ... </system.web> <appSettings> <add key="MyAppSetting" value="A test value." /> </appSettings> </configuration> 

Then this code can be used in your code by getting the value

 System.Configuration.ConfigurationManager.AppSettings["MyAppSetting"] 

(C #) or

 System.Configuration.ConfigurationManager.AppSettings("MyAppSetting") 

(Vb)

See MSDN for more information, or simply search the web for "asp.net AppSettings."

+16
source

If you don’t have a database to connect to (which I understood from your question), you don’t even need to have a <connectionStrings> section in Web.config . This section is only necessary if you intend to connect to the database.

If you are using a database, then connectionString varies depending on several factors, such as authentication type, database product (MS SQL Server, MySQL), driver type (ODBC, .NET), etc.

The "vendor name" will depend on the database product you are using. For example, for SQL Server "System.Data.SqlClient"

You can look at this site for a complete list of database products and connection strings suitable for each product for different types of authentication, drivers used, etc.

+1
source

For an ASP.NET 4.5 application I use appSettings to configure email. I also use connectionStrings

AppSettings parameters must be enabled before connectionStrings not before configSections

 <configuration> <configSections> <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </configSections> <appSettings> <add key="ContactEmail0" value=" service@davincispainting.com " /> <add key="ContactEmail1" value=" estimate@davincispainting.com " /> </appSettings> <connectionStrings> <!--<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-DAV3-20150302043828.mdf;Initial Catalog=aspnet-DAV3-20150302043828;Integrated Security=True" providerName="System.Data.SqlClient" />--> <!--<add name="connectionString" connectionString="data source=localhost;Initial Catalog=*****;User ID=sa;Password=*****;" providerName="System.Data.SqlClient" />--> <!--<add name="connectionString" connectionString="data source=localhost;Initial Catalog=Davincis3;User ID=*****;Password=*****;" providerName="System.Data.SqlClient" />--> <!--<add name="connectionString" connectionString="data source=DELLLAPTOP-PC\SQLSERVEREXPRESS;Initial Catalog=Davincis3;User ID=sa;Password=*****;" providerName="System.Data.SqlClient" />--> <add name="connectionString" connectionString="data source=DELLLAPTOP-PC\SQLEXPRESS;Initial Catalog=Davincis3;User ID=sa;Password=*****;" providerName="System.Data.SqlClient" /> </connectionStrings> ... 
0
source

All Articles