[英]How to store custom data in web.config?
I have the following method in my apiController: 我的apiController中具有以下方法:
public IEnumerable<something> GetData(DataProvider dataProvider)
{
return dataProvider.GetData();
}
What I need is to invoke this method from javascript and pass it a parameter of DataProvider derived type. 我需要从javascript调用此方法,并将其传递给DataProvider派生类型的参数。 I can handle this by passing string, eg "FirstProvider" and than write N number of if's in GetData() method to create an instance of proper type.
我可以通过传递字符串(例如“ FirstProvider”)来处理此问题,然后在GetData()方法中写入N个if来创建适当类型的实例。
But is there some way that I can write in web.config file something like: 但是有什么方法可以在web.config文件中编写类似以下内容的内容:
<DataProviders>
<type = FirstDataProvider, alias = "FirstProvider">
<type = SecondDataProvider, alias = "SecondProvider">
</DataProviders>
Change getData method to: 将getData方法更改为:
public IEnumerable<something> GetData(string dataProviderAlias)
{
// get provider type by it's alias from web congfig,
// then instantiated and call:
return dataProvider.GetData();
}
And then find and instantiate the type by it's alias? 然后通过别名查找并实例化类型?
Note : I accepted the answer below cause it's pointed me in a right direction, but msdn says that IConfigurationSectionHandler
is deprecated. 注意 :我接受了以下答案,因为它为我指明了正确的方向,但是msdn说不建议使用
IConfigurationSectionHandler
。
So I used ConfigurationSection
, ConfigurationElementCollection
, ConfigurationElement
classes instead to build custom config section. 因此,我改用
ConfigurationSection
, ConfigurationElementCollection
和ConfigurationElement
类来构建自定义配置部分。
First of all, you can only store valid xml in web.config. 首先,您只能在web.config中存储有效的xml。
<type = FirstDataProvider, alias = "FirstProvider">
is not valid xml. <type = FirstDataProvider, alias = "FirstProvider">
无效的xml。
Second, there are a lot of moving pieces. 其次,有很多动人的东西。 Please follow the steps carefully -
请仔细执行以下步骤-
Make sure you enter the proper namespace for DataProviders. 确保为数据提供者输入正确的名称空间。 type="YOUR_APPLICATION.DataProviders" .
type =“ YOUR_APPLICATION.DataProviders” 。
<configuration>
<configSections>
<section name="DataProviders" type="WebApplication2010.DataProviders"
requirePermission="false"/>
</configSections>
<DataProviders>
<Provider type="FirstDataProvider" alias="FirstProvider"/>
<Provider type="SecondDataProvider" alias="SecondProvider"/>
</DataProviders>
....
</configuration>
public class DataProviders : IConfigurationSectionHandler
{
private static bool _initialized;
public static List<Provider> _providers;
public object Create(object parent, object configContext, XmlNode section)
{
XmlNodeList providers = section.SelectNodes("Provider");
_providers = new List<Provider>();
foreach (XmlNode provider in providers)
{
_providers.Add(new Provider
{
Type = provider.Attributes["type"].Value,
Alias = provider.Attributes["alias"].Value,
});
}
return null;
}
public static void Init()
{
if (!_initialized)
{
ConfigurationManager.GetSection("DataProviders");
_initialized = true;
}
}
public static IEnumerable<Provider> GetData(string dataProviderAlias)
{
return _providers.Where(p => p.Alias == dataProviderAlias);
}
}
public class Provider
{
public string Type { get; set; }
public string Alias { get; set; }
}
For good design practice, you want to read data from web.config only once, and store them in static variables. 为了获得良好的设计实践,您只希望从web.config中读取一次数据,并将其存储在静态变量中。 Therefore, you want to initialize inside Application_BeginRequest of Global.asax.
因此,您想在Global.asax的Application_BeginRequest内部进行初始化。
public class Global : System.Web.HttpApplication
{
void Application_BeginRequest(object sender, EventArgs e)
{
DataProviders.Init();
}
}
var providers = DataProviders.GetData("FirstProvider").ToList();
You can store arbitrary data in web.config in the appSettings
element: 您可以在
appSettings
元素的web.config中存储任意数据:
<configuration>
<appSettings>
<add key="FirstAlias" value="FirstProvider" />
<add key="SecondAlias" value="SecondProvider" />
</appSettings>
</configuration>
And you can then read the values using: 然后,您可以使用以下命令读取值:
String firstAlias = System.Configuration.ConfigurationManager.AppSettings["FirstAlias"];
String secondAlias = System.Configuration.ConfigurationManager.AppSettings["SecondAlias"];
It's built-in. 它是内置的。 It's supported.
支持。 It's where you're supposed to store custom data.
在这里应该存储自定义数据。
Well, I'm not sure if I understand what you want to achieve, but to implement your idea you need a custom section handler. 好吧,我不确定我是否了解您要实现的目标,但是要实现您的想法,您需要一个自定义节处理程序。
http://msdn.microsoft.com/en-us/library/2tw134k3(v=vs.100).aspx http://msdn.microsoft.com/zh-CN/library/2tw134k3(v=vs.100).aspx
In case that you want to create a database connection for specific dataprovider, see this similar question: 如果要为特定的数据提供程序创建数据库连接,请参见以下类似问题:
ASP.NET: How to create a connection from a web.config ConnectionString? ASP.NET:如何从web.config ConnectionString创建连接?
In my case, I needed to store two byte[] variables in my Web.Config file. 就我而言,我需要在Web.Config文件中存储两个byte []变量。 Since it must be valid XML data, I simply stored the contents of the arrays like so:
由于它必须是有效的XML数据,因此我像这样简单地存储了数组的内容:
<appSettings>
<add key="Array1" value="0,1,2,3,4,5,6,7,8,9" />
<add key="Array2" value="0,1,2,3,4,5,6,7,8,9" />
</appSettings>
I then call a function that reads this into a C# string, split it into a string[], and parse each string element as a byte into a resulting byte[] and return it. 然后,我调用一个函数,将其读取为C#字符串,将其拆分为string [],然后将每个字符串元素作为字节解析为结果byte []并返回。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.