简体   繁体   English

从Sharepoint Web部件访问web.config

[英]Accessing web.config from Sharepoint web part

I have a VS 2008 web parts project - in this project is a web.config file: something like this: 我有一个VS 2008 Web部件项目-该项目中有一个web.config文件:

<?xml version="1.0"?>
<configuration>
  <connectionStrings/>
  <system.web>
    <appSettings>
      <add key="MFOwner" value="Blah" />
    </appSettings>
…….

In my web part I am trying to access values in the appSetting section: I've tried all of the code below and each returns null: 在我的Web部件中,我试图访问appSetting部分中的值:我已经尝试了以下所有代码,并且每个代码都返回null:

 string Owner = ConfigurationManager.AppSettings.Get("MFOwner");
 string stuff1 = ConfigurationManager.AppSettings["MFOwner"];
 string stuff3 = WebConfigurationManager.AppSettings["MFOwner"];
 string stuff4 = WebConfigurationManager.AppSettings.Get("MFOwner");
 string stuff2 = ConfigurationManager.AppSettings["MFowner".ToString()];

I've tried this code I found: 我已经尝试过以下找到的代码:

NameValueCollection sAll;
sAll = ConfigurationManager.AppSettings;
string a;
string b;
foreach (string s in sAll.AllKeys)
   {
     a = s;
     b = sAll.Get(s);
    }

and stepped through it in debug mode - that is getting things like : 并以调试模式逐步执行它-得到如下内容:

FeedCacheTimer FeedCacheTimer
FeedPageURL FeedPageURL
FeedXsl1 FeedXsl1
ReportViewerMessages ReportViewerMessages

which is NOT coming from anything in my web.config file....maybe a config file in sharepoint itself? 这不是来自我的web.config文件中的任何内容...。也许是sharepoint本身的配置文件? How do I access a web.config (or any other kind of config file!) local to my web part??? 如何访问Web部件本地的web.config(或任何其他类型的配置文件!)?

thanks, 谢谢,

Phil J 菲尔·J

Those values look like default SharePoint web.config properties. 这些值看起来像默认的SharePoint web.config属性。 When you create a SharePoint web part, it gets deployed to the IIS virtual directory that hosts the SharePoint site which has it's own (very large) web.config file and NOT your application web.config file. 创建SharePoint Web部件时,它将部署到承载SharePoint网站的IIS虚拟目录中,该站点具有自己的(很大)web.config文件,而不是您的应用程序web.config文件。

Depending on your production environment, you can update the SharePoint web.config file with the values you want and your code should work. 根据您的生产环境,可以使用所需的值更新SharePoint web.config文件,并且代码应可以正常工作。 If you cannot update the web.config file you can look into using a stand alone config file in the IIS directory that you deploy to (custom code then to read it) or possibly the property bag associated with each SP site collection for which you would need to add your values via code. 如果无法更新web.config文件,则可以使用部署到IIS目录中的独立配置文件(然后读取自定义代码)或可能与每个SP网站集相关联的属性包进行调查。需要通过代码添加您的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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