Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a string in a web.config and use it in a web.api

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

I found the following tutorials on how to do that :

Setting up connection string in ASP.NET to SQL SERVER

And

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

I have the following question , I don t have a database to connect the string to(I will only use it in the web config so that I can easily change the string without having to go through code . so assuming that I am using it in the following way :

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

What should I have in the connectionString and the providerName ?

like image 641
user1415780 Avatar asked Apr 08 '13 13:04

user1415780


1 Answers

If I understand what you want to do, it sounds like you don't want to use a connection string at all. Instead, use the Application Settings sections of your web.config file. For example

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

This can then be used in your code by getting the value of

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

(C#) or

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

(VB)

See MSDN for more information, or just search online for "asp.net AppSettings".

like image 60
karaken12 Avatar answered Oct 02 '22 08:10

karaken12