Accessing Web.Config from a separate solution

半城伤御伤魂 提交于 2019-12-25 12:35:09

问题


I have two solutions, one is a class library and the other is a web application, and I want to get the connection string from the web.config file to the class library as I am developing a custom membership provider. I am using Framework 4.0 and MS Visual Studio 2010.

Thanks


回答1:


You can put the configuration settings for any library in the main web.config. It's easy!

Connection strings are especially easy. Just add your connection string to the connectionstrings section with the same name it has in the library's app.config, and you're done!

<connectionStrings>
    <add name="Sitefinity" connectionString="your connection string"/>
</connectionStrings>

To add configuration settings, at the top of your web config, find the applicationSettings section, and add your section's info. Note: be sure to set your library's settings access modifier to "Public". You can do this in the Properties ui.

<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
  <section name="Your.Assembly" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>

Then, add the section under applicationSettings.

<applicationSettings>
<Your.Assembly>
  <setting name="TestSetting" serializeAs="String">
    <value>a test value</value>
  </setting>
</Your.Assembly>
</applicationSettings>


来源:https://stackoverflow.com/questions/9893577/accessing-web-config-from-a-separate-solution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!